forked from gravitational/teleport
/
impersonate.go
87 lines (79 loc) · 2.77 KB
/
impersonate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package services
import (
"strings"
"github.com/gravitational/trace"
"github.com/vulcand/predicate"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/api/types"
)
// impersonateContext is a default rule context used in teleport
type impersonateContext struct {
// user is currently authenticated user
user types.User
// impersonateRole is a role to impersonate
impersonateRole types.Role
// impersonateUser is a user to impersonate
impersonateUser types.User
}
// getIdentifier returns identifier defined in a context
func (ctx *impersonateContext) getIdentifier(fields []string) (interface{}, error) {
switch fields[0] {
case UserIdentifier:
return predicate.GetFieldByTag(ctx.user, teleport.JSON, fields[1:])
case ImpersonateUserIdentifier:
return predicate.GetFieldByTag(ctx.impersonateUser, teleport.JSON, fields[1:])
case ImpersonateRoleIdentifier:
return predicate.GetFieldByTag(ctx.impersonateRole, teleport.JSON, fields[1:])
default:
return nil, trace.NotFound("%v is not defined", strings.Join(fields, "."))
}
}
// matchesImpersonateWhere returns true if Where rule matches.
// Empty Where block always matches.
func matchesImpersonateWhere(cond types.ImpersonateConditions, parser predicate.Parser) (bool, error) {
if cond.Where == "" {
return true, nil
}
ifn, err := parser.Parse(cond.Where)
if err != nil {
return false, trace.Wrap(err)
}
fn, ok := ifn.(predicate.BoolPredicate)
if !ok {
return false, trace.BadParameter("invalid predicate type for where expression: %v", cond.Where)
}
return fn(), nil
}
// newImpersonateWhereParser returns standard parser for `where` section in impersonate rules
func newImpersonateWhereParser(ctx *impersonateContext) (predicate.Parser, error) {
return predicate.NewParser(predicate.Def{
Operators: predicate.Operators{
AND: predicate.And,
OR: predicate.Or,
NOT: predicate.Not,
},
Functions: map[string]interface{}{
"equals": predicate.Equals,
"contains": predicate.Contains,
},
GetIdentifier: ctx.getIdentifier,
GetProperty: GetStringMapValue,
})
}