-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adding where
and actions
section scripts in rules
#1222
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1fded08
scaffolding for rules matchers
klizhentas d7e5f7f
add tests
klizhentas 1be9f01
interface updates
klizhentas 393c67f
Merge branch 'master' into sasha/where2
klizhentas ee80f94
vendor dependencies
klizhentas 2a60416
tweak log action
klizhentas 699d1ad
fix error message
klizhentas 1537fa2
Merge branch 'master' into sasha/where2
klizhentas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,223 @@ | ||
/* | ||
Copyright 2017 Gravitational, Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package services | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/gravitational/teleport" | ||
|
||
"github.com/gravitational/trace" | ||
"github.com/jonboulle/clockwork" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/vulcand/predicate" | ||
) | ||
|
||
// RuleContext specifies context passed to the | ||
// rule processing matcher, and contains information | ||
// about current session, e.g. current user | ||
type RuleContext interface { | ||
// GetIdentifier returns identifier defined in a context | ||
GetIdentifier(fields []string) (interface{}, error) | ||
// String returns human friendly representation of a context | ||
String() string | ||
} | ||
|
||
// NewWhereParser returns standard parser for `where` section in access rules | ||
func NewWhereParser(ctx RuleContext) (predicate.Parser, error) { | ||
return predicate.NewParser(predicate.Def{ | ||
Operators: predicate.Operators{ | ||
AND: predicate.And, | ||
OR: predicate.Or, | ||
}, | ||
Functions: map[string]interface{}{ | ||
"equals": predicate.Equals, | ||
"contains": predicate.Contains, | ||
}, | ||
GetIdentifier: ctx.GetIdentifier, | ||
GetProperty: predicate.GetStringMapValue, | ||
}) | ||
} | ||
|
||
// NewActionsParser returns standard parser for 'actions' section in access rules | ||
func NewActionsParser(ctx RuleContext) (predicate.Parser, error) { | ||
return predicate.NewParser(predicate.Def{ | ||
Operators: predicate.Operators{}, | ||
Functions: map[string]interface{}{ | ||
"log": NewLogActionFn(ctx), | ||
}, | ||
GetIdentifier: ctx.GetIdentifier, | ||
GetProperty: predicate.GetStringMapValue, | ||
}) | ||
} | ||
|
||
// NewLogActionFn creates logger functions | ||
func NewLogActionFn(ctx RuleContext) interface{} { | ||
return (&LogAction{ctx: ctx}).Log | ||
} | ||
|
||
// LogAction represents action that will emit log entry | ||
// when specified in the actions of a matched rule | ||
type LogAction struct { | ||
ctx RuleContext | ||
} | ||
|
||
// Log logs with specified level and formatting string with arguments | ||
func (l *LogAction) Log(level, format string, args ...interface{}) predicate.BoolPredicate { | ||
return func() bool { | ||
ilevel, err := log.ParseLevel(level) | ||
if err != nil { | ||
ilevel = log.DebugLevel | ||
} | ||
writer := log.StandardLogger().WriterLevel(ilevel) | ||
writer.Write([]byte(fmt.Sprintf(format, args...))) | ||
return true | ||
} | ||
} | ||
|
||
// Context is a default rule context used in teleport | ||
type Context struct { | ||
// User is currently authenticated user | ||
User User | ||
// Resource is an optional resource, in case if the rule | ||
// checks access to the resource | ||
Resource Resource | ||
} | ||
|
||
// String returns user friendly representation of this context | ||
func (ctx *Context) String() string { | ||
return fmt.Sprintf("user %v, resource: %v", ctx.User, ctx.Resource) | ||
} | ||
|
||
const ( | ||
// UserIdentifier represents user registered identifier in the rules | ||
UserIdentifier = "user" | ||
// ResourceIdentifier represents resource registered identifer in the rules | ||
ResourceIdentifier = "resource" | ||
) | ||
|
||
// GetIdentifier returns identifier defined in a context | ||
func (ctx *Context) GetIdentifier(fields []string) (interface{}, error) { | ||
switch fields[0] { | ||
case UserIdentifier: | ||
var user User | ||
if ctx.User == nil { | ||
user = emptyUser | ||
} else { | ||
user = ctx.User | ||
} | ||
return predicate.GetFieldByTag(user, teleport.JSON, fields[1:]) | ||
case ResourceIdentifier: | ||
var resource Resource | ||
if ctx.Resource == nil { | ||
resource = emptyResource | ||
} else { | ||
resource = ctx.Resource | ||
} | ||
return predicate.GetFieldByTag(resource, "json", fields[1:]) | ||
default: | ||
return nil, trace.NotFound("%v is not defined", strings.Join(fields, ".")) | ||
} | ||
} | ||
|
||
// NewParserFn returns function that creates parser of 'where' section | ||
// in access rules | ||
type NewParserFn func(ctx RuleContext) (predicate.Parser, error) | ||
|
||
var whereParser = NewWhereParser | ||
var actionsParser = NewActionsParser | ||
|
||
// GetWhereParserFn returns global function that creates where parsers | ||
// this function is used in external tools to override and extend 'where' in rules | ||
func GetWhereParserFn() NewParserFn { | ||
marshalerMutex.RLock() | ||
defer marshalerMutex.RUnlock() | ||
return whereParser | ||
} | ||
|
||
// SetWhereParserFn sets global function that creates where parsers | ||
// this function is used in external tools to override and extend 'where' in rules | ||
func SetWhereParserFn(fn NewParserFn) { | ||
marshalerMutex.Lock() | ||
defer marshalerMutex.Unlock() | ||
whereParser = fn | ||
} | ||
|
||
// GetActionsParserFn returns global function that creates where parsers | ||
// this function is used in external tools to override and extend actions in rules | ||
func GetActionsParserFn() NewParserFn { | ||
marshalerMutex.RLock() | ||
defer marshalerMutex.RUnlock() | ||
return actionsParser | ||
} | ||
|
||
// SetActionsParserFn sets global function that creates actions parsers | ||
// this function is used in external tools to override and extend actions in rules | ||
func SetActionsParserFn(fn NewParserFn) { | ||
marshalerMutex.Lock() | ||
defer marshalerMutex.Unlock() | ||
actionsParser = fn | ||
} | ||
|
||
// emptyResource is used when no resource is specified | ||
var emptyResource = &EmptyResource{} | ||
|
||
// emptyUser is used when no user is specified | ||
var emptyUser = &UserV2{} | ||
|
||
// EmptyResource is used to represent a use case when no resource | ||
// is specified in the rules matcher | ||
type EmptyResource struct { | ||
// Kind is a resource kind | ||
Kind string `json:"kind"` | ||
// Version is a resource version | ||
Version string `json:"version"` | ||
// Metadata is Role metadata | ||
Metadata Metadata `json:"metadata"` | ||
} | ||
|
||
// SetExpiry sets expiry time for the object. | ||
func (r *EmptyResource) SetExpiry(expires time.Time) { | ||
r.Metadata.SetExpiry(expires) | ||
} | ||
|
||
// Expiry returns the expiry time for the object. | ||
func (r *EmptyResource) Expiry() time.Time { | ||
return r.Metadata.Expiry() | ||
} | ||
|
||
// SetTTL sets TTL header using realtime clock. | ||
func (r *EmptyResource) SetTTL(clock clockwork.Clock, ttl time.Duration) { | ||
r.Metadata.SetTTL(clock, ttl) | ||
} | ||
|
||
// SetName sets the role name and is a shortcut for SetMetadata().Name. | ||
func (r *EmptyResource) SetName(s string) { | ||
r.Metadata.Name = s | ||
} | ||
|
||
// GetName gets the role name and is a shortcut for GetMetadata().Name. | ||
func (r *EmptyResource) GetName() string { | ||
return r.Metadata.Name | ||
} | ||
|
||
// GetMetadata returns role metadata. | ||
func (r *EmptyResource) GetMetadata() Metadata { | ||
return r.Metadata | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If users can directly access the internal representation of Teleport objects like this
contains(user.spec.traits["groups"], "prod")
, that will make migration difficult if/when we change the internal representation of a resource.For example if we remove or rename
traits
in the future, roles will break. No only that, becausewhere
is a string, migrating it will be non-trivial.What do you think about a Context with predefined fields in it. That way migrations will be easier in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand your suggestion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@russjones these are not internal representation of teleport objects, but the API of the versioned objects exposed to the user.