-
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
Dynamic Roles #897
Merged
Merged
Dynamic Roles #897
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
81ba372
Added support for dynamic roles in claim mappings for OIDC.
russjones fde0ae4
Added TTL to UpsertRole. Dynamic roles for OIDC users now have TTL.
russjones fcfb883
Update exisiting OIDC users upon re-login.
russjones 72ecdb8
Code review fixes.
russjones 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
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 |
---|---|---|
|
@@ -28,7 +28,6 @@ import ( | |
"fmt" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
|
@@ -779,13 +778,41 @@ type OIDCAuthResponse struct { | |
HostSigners []services.CertAuthority `json:"host_signers"` | ||
} | ||
|
||
func (a *AuthServer) createOIDCUser(connector services.OIDCConnector, ident *oidc.Identity, claims jose.Claims) error { | ||
// buildRoles takes a connector and claims and returns a slice of roles. If the claims | ||
// match a concrete roles in the connector, those roles are returned directly. If the | ||
// claims match a template role in the connector, then that role is first created from | ||
// the template, then returned. | ||
func (a *AuthServer) buildRoles(connector services.OIDCConnector, ident *oidc.Identity, claims jose.Claims) ([]string, error) { | ||
roles := connector.MapClaims(claims) | ||
if len(roles) == 0 { | ||
log.Warningf("[OIDC] could not find any of expected claims: %v in the set returned by provider %v: %v", | ||
strings.Join(connector.GetClaims(), ","), connector.GetName(), strings.Join(services.GetClaimNames(claims), ",")) | ||
return trace.AccessDenied("access denied to %v", ident.Email) | ||
role, err := connector.RoleFromTemplate(claims) | ||
if err != nil { | ||
log.Warningf("[OIDC] Unable to map claims to roles or role templates for %q", connector.GetName()) | ||
return nil, trace.AccessDenied("unable to map claims to roles or role templates for %q", connector.GetName()) | ||
} | ||
|
||
// figure out ttl for role. expires = now + ttl => ttl = expires - now | ||
ttl := ident.ExpiresAt.Sub(time.Now()) | ||
|
||
// upsert templated role | ||
err = a.Access.UpsertRole(role, ttl) | ||
if err != nil { | ||
log.Warningf("[OIDC] Unable to upsert templated role for connector: %q", connector.GetName()) | ||
return nil, trace.AccessDenied("unable to upsert templated role: %q", connector.GetName()) | ||
} | ||
|
||
roles = []string{role.GetName()} | ||
} | ||
|
||
return roles, nil | ||
} | ||
|
||
func (a *AuthServer) createOIDCUser(connector services.OIDCConnector, ident *oidc.Identity, claims jose.Claims) error { | ||
roles, err := a.buildRoles(connector, ident, claims) | ||
if err != nil { | ||
return trace.Wrap(err) | ||
} | ||
|
||
log.Debugf("[IDENTITY] %v/%v is a dynamic identity, generating user with roles: %v", connector.GetName(), ident.Email, roles) | ||
user, err := services.GetUserMarshaler().GenerateUser(&services.UserV2{ | ||
Kind: services.KindUser, | ||
|
@@ -812,25 +839,30 @@ func (a *AuthServer) createOIDCUser(connector services.OIDCConnector, ident *oid | |
if err != nil { | ||
return trace.Wrap(err) | ||
} | ||
err = a.CreateUser(user) | ||
if err == nil { | ||
return trace.Wrap(err) | ||
} | ||
if !trace.IsAlreadyExists(err) { | ||
return trace.Wrap(err) | ||
} | ||
|
||
// check if a user exists already | ||
existingUser, err := a.GetUser(ident.Email) | ||
if err != nil { | ||
if !trace.IsNotFound(err) { | ||
return trace.Wrap(err) | ||
} | ||
} else { | ||
} | ||
|
||
// check if any exisiting user is a non-oidc user, dont override their | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. incomplete comment sentence |
||
if existingUser != nil { | ||
connectorRef := existingUser.GetCreatedBy().Connector | ||
if connectorRef == nil || connectorRef.Type != teleport.ConnectorOIDC || connectorRef.ID != connector.GetName() { | ||
return trace.AlreadyExists("user %v already exists and is not OIDC user", existingUser.GetName()) | ||
return trace.AlreadyExists("user %q already exists and is not OIDC user", existingUser.GetName()) | ||
} | ||
} | ||
return a.UpsertUser(user) | ||
|
||
// no non-oidc user exists, create or update the exisiting oidc user | ||
err = a.UpsertUser(user) | ||
if err != nil { | ||
return trace.Wrap(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// claimsFromIDToken extracts claims from the ID token. | ||
|
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 |
---|---|---|
|
@@ -17,6 +17,7 @@ limitations under the License. | |
package auth | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
|
@@ -28,9 +29,12 @@ import ( | |
"github.com/gravitational/teleport/lib/services" | ||
"github.com/gravitational/teleport/lib/services/suite" | ||
"github.com/gravitational/teleport/lib/utils" | ||
"github.com/jonboulle/clockwork" | ||
|
||
"github.com/gravitational/trace" | ||
|
||
"github.com/coreos/go-oidc/jose" | ||
"github.com/coreos/go-oidc/oidc" | ||
"github.com/jonboulle/clockwork" | ||
. "gopkg.in/check.v1" | ||
) | ||
|
||
|
@@ -42,6 +46,7 @@ type AuthSuite struct { | |
} | ||
|
||
var _ = Suite(&AuthSuite{}) | ||
var _ = fmt.Printf | ||
|
||
func (s *AuthSuite) SetUpSuite(c *C) { | ||
utils.InitLoggerForTests() | ||
|
@@ -212,3 +217,124 @@ func (s *AuthSuite) TestBadTokens(c *C) { | |
_, err = s.a.ValidateToken(tampered) | ||
c.Assert(err, NotNil) | ||
} | ||
|
||
func (s *AuthSuite) TestBuildRolesInvalid(c *C) { | ||
// create a connector | ||
oidcConnector := services.NewOIDCConnector("example", services.OIDCConnectorSpecV2{ | ||
IssuerURL: "https://www.exmaple.com", | ||
ClientID: "example-client-id", | ||
ClientSecret: "example-client-secret", | ||
RedirectURL: "https://localhost:3080/v1/webapi/oidc/callback", | ||
Display: "sign in with example.com", | ||
Scope: []string{"foo", "bar"}, | ||
}) | ||
|
||
// create some claims | ||
var claims = make(jose.Claims) | ||
claims.Add("roles", "teleport-user") | ||
claims.Add("email", "[email protected]") | ||
claims.Add("nickname", "foo") | ||
claims.Add("full_name", "foo bar") | ||
|
||
// create an identity for the ttl | ||
ident := &oidc.Identity{ | ||
ExpiresAt: time.Now().Add(1 * time.Minute), | ||
} | ||
|
||
// try and build roles should be invalid since we have no mappings | ||
_, err := s.a.buildRoles(oidcConnector, ident, claims) | ||
c.Assert(err, NotNil) | ||
} | ||
|
||
func (s *AuthSuite) TestBuildRolesStatic(c *C) { | ||
// create a connector | ||
oidcConnector := services.NewOIDCConnector("example", services.OIDCConnectorSpecV2{ | ||
IssuerURL: "https://www.exmaple.com", | ||
ClientID: "example-client-id", | ||
ClientSecret: "example-client-secret", | ||
RedirectURL: "https://localhost:3080/v1/webapi/oidc/callback", | ||
Display: "sign in with example.com", | ||
Scope: []string{"foo", "bar"}, | ||
ClaimsToRoles: []services.ClaimMapping{ | ||
services.ClaimMapping{ | ||
Claim: "roles", | ||
Value: "teleport-user", | ||
Roles: []string{"user"}, | ||
}, | ||
}, | ||
}) | ||
|
||
// create some claims | ||
var claims = make(jose.Claims) | ||
claims.Add("roles", "teleport-user") | ||
claims.Add("email", "[email protected]") | ||
claims.Add("nickname", "foo") | ||
claims.Add("full_name", "foo bar") | ||
|
||
// create an identity for the ttl | ||
ident := &oidc.Identity{ | ||
ExpiresAt: time.Now().Add(1 * time.Minute), | ||
} | ||
|
||
// build roles and check that we mapped to "user" role | ||
roles, err := s.a.buildRoles(oidcConnector, ident, claims) | ||
c.Assert(err, IsNil) | ||
c.Assert(roles, HasLen, 1) | ||
c.Assert(roles[0], Equals, "user") | ||
} | ||
|
||
func (s *AuthSuite) TestBuildRolesTemplate(c *C) { | ||
// create a connector | ||
oidcConnector := services.NewOIDCConnector("example", services.OIDCConnectorSpecV2{ | ||
IssuerURL: "https://www.exmaple.com", | ||
ClientID: "example-client-id", | ||
ClientSecret: "example-client-secret", | ||
RedirectURL: "https://localhost:3080/v1/webapi/oidc/callback", | ||
Display: "sign in with example.com", | ||
Scope: []string{"foo", "bar"}, | ||
ClaimsToRoles: []services.ClaimMapping{ | ||
services.ClaimMapping{ | ||
Claim: "roles", | ||
Value: "teleport-user", | ||
RoleTemplate: &services.RoleV2{ | ||
Kind: services.KindRole, | ||
Version: services.V2, | ||
Metadata: services.Metadata{ | ||
Name: `{{index . "email"}}`, | ||
Namespace: defaults.Namespace, | ||
}, | ||
Spec: services.RoleSpecV2{ | ||
MaxSessionTTL: services.NewDuration(90 * 60 * time.Minute), | ||
Logins: []string{`{{index . "nickname"}}`, `root`}, | ||
NodeLabels: map[string]string{"*": "*"}, | ||
Namespaces: []string{"*"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
// create some claims | ||
var claims = make(jose.Claims) | ||
claims.Add("roles", "teleport-user") | ||
claims.Add("email", "[email protected]") | ||
claims.Add("nickname", "foo") | ||
claims.Add("full_name", "foo bar") | ||
|
||
// create an identity for the ttl | ||
ident := &oidc.Identity{ | ||
ExpiresAt: time.Now().Add(1 * time.Minute), | ||
} | ||
|
||
// build roles | ||
roles, err := s.a.buildRoles(oidcConnector, ident, claims) | ||
c.Assert(err, IsNil) | ||
|
||
// check that the newly created role was both returned and upserted into the backend | ||
r, err := s.a.GetRoles() | ||
c.Assert(err, IsNil) | ||
c.Assert(r, HasLen, 1) | ||
c.Assert(r[0].GetName(), Equals, "[email protected]") | ||
c.Assert(roles, HasLen, 1) | ||
c.Assert(roles[0], Equals, "[email protected]") | ||
} |
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
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
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.
can you use auth server clock instead?