mirror of
https://github.com/gravitational/teleport.git
synced 2026-09-21 14:35:22 +08:00
* Add API types for CircleCI joining * Add validation for CircleCI configuration * Add JoinMethodCircleCI across codebase * Add token validator and token source for circleci * Update join methods RFD * Add CircleCI token source to register.go * Add serverside support for circleci joining * Add test for TokenSource * Add success case for token validation test * Add expired test case * Add test case for token from another org * Test RegisterWithToken for CircleCI * Refactor GitHub RegisterUsingToken tests * Refactor CircleCI RegisterUsingToken tests * Add tests for ProvisionTokenSpecV2 * Appease linters * Go Imports files * Fix failing test for msising IDtoken * Update lib/auth/join_circleci.go Co-authored-by: Zac Bergquist <zac.bergquist@goteleport.com> * Move test server cleanup closer to initialisation * Fix weird import reordering * Include unexpected type in error message * Simplify boolean algebra :) Co-authored-by: Zac Bergquist <zac.bergquist@goteleport.com>
96 lines
2.7 KiB
Go
96 lines
2.7 KiB
Go
/*
|
|
Copyright 2022 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 auth
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gravitational/trace"
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/gravitational/teleport/api/types"
|
|
"github.com/gravitational/teleport/lib/githubactions"
|
|
)
|
|
|
|
type ghaIDTokenValidator interface {
|
|
Validate(context.Context, string) (*githubactions.IDTokenClaims, error)
|
|
}
|
|
|
|
func (a *Server) checkGitHubJoinRequest(ctx context.Context, req *types.RegisterUsingTokenRequest) error {
|
|
if req.IDToken == "" {
|
|
return trace.BadParameter("IDToken not provided for Github join request")
|
|
}
|
|
pt, err := a.GetToken(ctx, req.Token)
|
|
if err != nil {
|
|
return trace.Wrap(err)
|
|
}
|
|
|
|
claims, err := a.ghaIDTokenValidator.Validate(ctx, req.IDToken)
|
|
if err != nil {
|
|
return trace.Wrap(err)
|
|
}
|
|
|
|
log.WithFields(logrus.Fields{
|
|
"claims": claims,
|
|
"token": pt.GetName(),
|
|
}).Info("Github actions run trying to join cluster")
|
|
|
|
return trace.Wrap(checkGithubAllowRules(pt, claims))
|
|
}
|
|
|
|
func checkGithubAllowRules(pt types.ProvisionToken, claims *githubactions.IDTokenClaims) error {
|
|
token, ok := pt.(*types.ProvisionTokenV2)
|
|
if !ok {
|
|
return trace.BadParameter("github join method only supports ProvisionTokenV2, '%T' was provided", pt)
|
|
}
|
|
|
|
// If a single rule passes, accept the IDToken
|
|
for _, rule := range token.Spec.GitHub.Allow {
|
|
// Please consider keeping these field validators in the same order they
|
|
// are defined within the ProvisionTokenSpecV2Github proto spec.
|
|
if rule.Sub != "" && claims.Sub != rule.Sub {
|
|
continue
|
|
}
|
|
if rule.Repository != "" && claims.Repository != rule.Repository {
|
|
continue
|
|
}
|
|
if rule.RepositoryOwner != "" && claims.RepositoryOwner != rule.RepositoryOwner {
|
|
continue
|
|
}
|
|
if rule.Workflow != "" && claims.Workflow != rule.Workflow {
|
|
continue
|
|
}
|
|
if rule.Environment != "" && claims.Environment != rule.Environment {
|
|
continue
|
|
}
|
|
if rule.Actor != "" && claims.Actor != rule.Actor {
|
|
continue
|
|
}
|
|
if rule.Ref != "" && claims.Ref != rule.Ref {
|
|
continue
|
|
}
|
|
if rule.RefType != "" && claims.RefType != rule.RefType {
|
|
continue
|
|
}
|
|
|
|
// All provided rules met.
|
|
return nil
|
|
}
|
|
|
|
return trace.AccessDenied("id token claims did not match any allow rules")
|
|
}
|