Skip to content

Commit

Permalink
Tidy up naming
Browse files Browse the repository at this point in the history
  • Loading branch information
strideynet committed Nov 29, 2024
1 parent 8abdeab commit cd3222c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/auth/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func (a *Server) updateBotInstance(
if templateAuthRecord != nil {
authRecord.JoinToken = templateAuthRecord.JoinToken
authRecord.JoinMethod = templateAuthRecord.JoinMethod
authRecord.Metadata = templateAuthRecord.Metadata
authRecord.JoinAttrs = templateAuthRecord.JoinAttrs
}

// An empty bot instance most likely means a bot is rejoining after an
Expand Down
48 changes: 25 additions & 23 deletions lib/auth/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func setRemoteAddrFromContext(ctx context.Context, req *types.RegisterUsingToken
func (a *Server) handleJoinFailure(
origErr error,
pt types.ProvisionToken,
attributes any,
rawJoinAttrs any,
req *types.RegisterUsingTokenRequest,
) {
fields := logrus.Fields{}
Expand All @@ -129,10 +129,13 @@ func (a *Server) handleJoinFailure(
fields["remote_addr"] = req.RemoteAddr
}

// Fetch and encode attributes if they are available.
attributesStruct, err := untypedAttrsToStruct(attributes)
// Fetch and encode rawJoinAttrs if they are available.
attributesStruct, err := rawJoinAttrsToStruct(rawJoinAttrs)
if err != nil {
log.WithError(err).Warn("Unable to encode join attributes for audit event.")
log.WithError(err).Warn("Unable to encode join rawJoinAttrs for audit event.")
}
if attributesStruct != nil {
fields["attributes"] = attributesStruct
}

// Add log fields from token if available.
Expand Down Expand Up @@ -204,15 +207,14 @@ func (a *Server) handleJoinFailure(
// will be checked.
func (a *Server) RegisterUsingToken(ctx context.Context, req *types.RegisterUsingTokenRequest) (certs *proto.Certs, err error) {
attrs := &workloadidentityv1pb.JoinAttrs{}
// untypedAttrs holds the unstructured join attributes specific to that
// join method for the purposes of including in the audit logs.
// Realistically, this can hold anything that can be JSON marshaled.
var untypedAttrs any
// rawJoinAttrs typically holds the raw metadata sourced from a join.
// E.g the claims from a JWT token. This is used for auditing purposes.
var rawJoinAttrs any
var provisionToken types.ProvisionToken
defer func() {
// Emit a log message and audit event on join failure.
if err != nil {
a.handleJoinFailure(err, provisionToken, untypedAttrs, req)
a.handleJoinFailure(err, provisionToken, rawJoinAttrs, req)
}
}()

Expand All @@ -234,7 +236,7 @@ func (a *Server) RegisterUsingToken(ctx context.Context, req *types.RegisterUsin
case types.JoinMethodGitHub:
claims, err := a.checkGitHubJoinRequest(ctx, req)
if claims != nil {
untypedAttrs = claims
rawJoinAttrs = claims
attrs.Github = claims.JoinAttrs()
}
if err != nil {
Expand All @@ -243,7 +245,7 @@ func (a *Server) RegisterUsingToken(ctx context.Context, req *types.RegisterUsin
case types.JoinMethodGitLab:
claims, err := a.checkGitLabJoinRequest(ctx, req)
if claims != nil {
untypedAttrs = claims
rawJoinAttrs = claims
attrs.Gitlab = claims.JoinAttrs()
}
if err != nil {
Expand All @@ -252,47 +254,47 @@ func (a *Server) RegisterUsingToken(ctx context.Context, req *types.RegisterUsin
case types.JoinMethodCircleCI:
claims, err := a.checkCircleCIJoinRequest(ctx, req)
if claims != nil {
untypedAttrs = claims
rawJoinAttrs = claims
}
if err != nil {
return nil, trace.Wrap(err)
}
case types.JoinMethodKubernetes:
claims, err := a.checkKubernetesJoinRequest(ctx, req)
if claims != nil {
untypedAttrs = claims
rawJoinAttrs = claims
}
if err != nil {
return nil, trace.Wrap(err)
}
case types.JoinMethodGCP:
claims, err := a.checkGCPJoinRequest(ctx, req)
if claims != nil {
untypedAttrs = claims
rawJoinAttrs = claims
}
if err != nil {
return nil, trace.Wrap(err)
}
case types.JoinMethodSpacelift:
claims, err := a.checkSpaceliftJoinRequest(ctx, req)
if claims != nil {
untypedAttrs = claims
rawJoinAttrs = claims
}
if err != nil {
return nil, trace.Wrap(err)
}
case types.JoinMethodTerraformCloud:
claims, err := a.checkTerraformCloudJoinRequest(ctx, req)
if claims != nil {
untypedAttrs = claims
rawJoinAttrs = claims
}
if err != nil {
return nil, trace.Wrap(err)
}
case types.JoinMethodBitbucket:
claims, err := a.checkBitbucketJoinRequest(ctx, req)
if claims != nil {
untypedAttrs = claims
rawJoinAttrs = claims
}
if err != nil {
return nil, trace.Wrap(err)
Expand All @@ -319,20 +321,20 @@ func (a *Server) RegisterUsingToken(ctx context.Context, req *types.RegisterUsin
ctx,
provisionToken,
req,
untypedAttrs,
rawJoinAttrs,
attrs,
)
return certs, trace.Wrap(err)
}
certs, err = a.generateCerts(ctx, provisionToken, req, untypedAttrs)
certs, err = a.generateCerts(ctx, provisionToken, req, rawJoinAttrs)
return certs, trace.Wrap(err)
}

func (a *Server) generateCertsBot(
ctx context.Context,
provisionToken types.ProvisionToken,
req *types.RegisterUsingTokenRequest,
untypedAttrs any,
rawJoinAttrs any,
attrs *workloadidentityv1pb.JoinAttrs,
) (*proto.Certs, error) {
// bots use this endpoint but get a user cert
Expand Down Expand Up @@ -382,7 +384,7 @@ func (a *Server) generateCertsBot(
},
}
var err error
joinEvent.Attributes, err = untypedAttrsToStruct(untypedAttrs)
joinEvent.Attributes, err = rawJoinAttrsToStruct(rawJoinAttrs)
if err != nil {
log.WithError(err).Warn("Unable to encode join attributes for audit event.")
}
Expand Down Expand Up @@ -511,7 +513,7 @@ func (a *Server) generateCerts(
RemoteAddr: req.RemoteAddr,
},
}
joinEvent.Attributes, err = untypedAttrsToStruct(untypedAttrs)
joinEvent.Attributes, err = rawJoinAttrsToStruct(untypedAttrs)
if err != nil {
log.WithError(err).Warn("Unable to encode join attributes for audit event.")
}
Expand All @@ -521,7 +523,7 @@ func (a *Server) generateCerts(
return certs, nil
}

func untypedAttrsToStruct(in any) (*apievents.Struct, error) {
func rawJoinAttrsToStruct(in any) (*apievents.Struct, error) {
if in == nil {
return nil, nil
}
Expand Down

0 comments on commit cd3222c

Please sign in to comment.