Files
teleport/lib/web/ui/usercontext.go
T
3c670d5d58 Merge Teleport V4.3 UI branch to master (#3583)
* Add monorepo

* Add reset/passwd capability for local users (#3287)

* Add UserTokens to allow password resets

* Pass context down through ChangePasswordWithToken

* Rename UserToken to ResetPasswordToken

* Add auto formatting for proto files

* Add common Marshaller interfaces to reset password token

* Allow enterprise "tctl" reuse OSS user methods (#3344)

* Pass localAuthEnabled flag to UI (#3412)

* Added LocalAuthEnabled prop to WebConfigAuthSetting struct in webconfig.go
* Added LocalAuthEnabled state as part of webCfg in  apiserver.go

* update e-refs

* Fix a regression bug after merge

* Update tctl CLI output msgs (#3442)

* Use local user client when resolving user roles

* Update webapps ref

* Add and retrieve fields from Cluster struct (#3476)

* Set Teleport versions for node, auth, proxy init heartbeat
* Add and retrieve fields NodeCount, PublicURL, AuthVersion from Clusters
* Remove debug logging to avoid log pollution when getting public_addr of proxy
* Create helper func GuessProxyHost to get the public_addr of a proxy host
* Refactor newResetPasswordToken to use GuessProxyHost and remove publicUrl func

* Remove webapps submodule

* Add webassets submodule

* Replace webapps sub-module reference with webassets

* Update webassets path in Makefile

* Update webassets

1b11b26 Simplify and clean up Makefile (#62) https://github.com/gravitational/webapps/commit/1b11b26

* Retrieve cluster details for user context (#3515)

* Let GuessProxyHost also return proxy's version
* Unit test GuessProxyHostAndVersion & GetClusterDetails

* Update webassets

4dfef4e Fix build pipeline (#66) https://github.com/gravitational/webapps/commit/4dfef4e

* Update e-ref

* Update webassets

0647568 Fix OSS redirects https://github.com/gravitational/webapps/commit/0647568

* update e-ref

* Update webassets

e0f4189 Address security audit warnings Updates  "minimist" package which is used by 7y old "optimist". https://github.com/gravitational/webapps/commit/e0f4189

* Add new attr to Session struct (#3574)

* Add fields ServerHostname and ServerAddr
* Set these fields on newSession

* Ensure webassets submodule during build

* Update e-ref

* Ensure webassets before running unit-tests

* Update E-ref

Co-authored-by: Lisa Kim <lisa@gravitational.com>
Co-authored-by: Pierre Beaucamp <pierre@gravitational.com>
Co-authored-by: Jenkins <jenkins@gravitational.io>
2020-04-15 15:35:26 -04:00

144 lines
4.1 KiB
Go

/*
Copyright 2015 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 ui
import (
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/utils"
)
type access struct {
List bool `json:"list"`
Read bool `json:"read"`
Edit bool `json:"edit"`
Create bool `json:"create"`
Delete bool `json:"remove"`
}
type userACL struct {
// Sessions defines access to recorded sessions
Sessions access `json:"sessions"`
// AuthConnectors defines access to auth.connectors
AuthConnectors access `json:"authConnectors"`
// Roles defines access to roles
Roles access `json:"roles"`
// TrustedClusters defines access to trusted clusters
TrustedClusters access `json:"trustedClusters"`
// SSH defines access to servers
SSHLogins []string `json:"sshLogins"`
}
type authType string
const (
authLocal authType = "local"
authSSO authType = "sso"
)
type userContext struct {
// AuthType is auth method of this user
AuthType authType `json:"authType"`
// Name is this user name
Name string `json:"userName"`
// ACL contains user access control list
ACL userACL `json:"userAcl"`
// Cluster contains cluster detail for this user's context
Cluster *Cluster `json:"cluster"`
}
func getLogins(roleSet services.RoleSet) []string {
allowed := []string{}
denied := []string{}
for _, role := range roleSet {
denied = append(denied, role.GetLogins(services.Deny)...)
allowed = append(allowed, role.GetLogins(services.Allow)...)
}
allowed = utils.Deduplicate(allowed)
denied = utils.Deduplicate(denied)
userLogins := []string{}
for _, login := range allowed {
loginMatch, _ := services.MatchLogin(denied, login)
if loginMatch == false {
userLogins = append(userLogins, login)
}
}
return userLogins
}
func hasAccess(roleSet services.RoleSet, ctx *services.Context, kind string, verbs ...string) bool {
for _, verb := range verbs {
// Since this check occurs often and it does not imply the caller is trying
// to access any resource, silence any logging done on the proxy.
err := roleSet.CheckAccessToRule(ctx, defaults.Namespace, kind, verb, true)
if err != nil {
return false
}
}
return true
}
func newAccess(roleSet services.RoleSet, ctx *services.Context, kind string) access {
return access{
List: hasAccess(roleSet, ctx, kind, services.VerbList),
Read: hasAccess(roleSet, ctx, kind, services.VerbRead),
Edit: hasAccess(roleSet, ctx, kind, services.VerbUpdate),
Create: hasAccess(roleSet, ctx, kind, services.VerbCreate),
Delete: hasAccess(roleSet, ctx, kind, services.VerbDelete),
}
}
// NewUserContext returns user context
func NewUserContext(user services.User, userRoles services.RoleSet) (*userContext, error) {
ctx := &services.Context{User: user}
sessionAccess := newAccess(userRoles, ctx, services.KindSession)
roleAccess := newAccess(userRoles, ctx, services.KindRole)
authConnectors := newAccess(userRoles, ctx, services.KindAuthConnector)
trustedClusterAccess := newAccess(userRoles, ctx, services.KindTrustedCluster)
logins := getLogins(userRoles)
acl := userACL{
AuthConnectors: authConnectors,
TrustedClusters: trustedClusterAccess,
Sessions: sessionAccess,
Roles: roleAccess,
SSHLogins: logins,
}
// local user
authType := authLocal
// check for any SSO identities
isSSO := len(user.GetOIDCIdentities()) > 0 ||
len(user.GetGithubIdentities()) > 0 ||
len(user.GetSAMLIdentities()) > 0
if isSSO {
// SSO user
authType = authSSO
}
return &userContext{
Name: user.GetName(),
ACL: acl,
AuthType: authType,
}, nil
}