Skip to content

Commit

Permalink
resolve failing test on account_test.go
Browse files Browse the repository at this point in the history
Signed-off-by: Atif Ali <atali@redhat.com>
  • Loading branch information
aali309 committed Nov 14, 2024
1 parent 8d1237d commit b344eae
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 23 deletions.
1 change: 1 addition & 0 deletions server/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (s *Server) UpdatePassword(ctx context.Context, q *account.UpdatePasswordRe
// check for permission is user is trying to change someone else's password
// assuming user is trying to update someone else if username is different or issuer is not Argo CD
if updatedUsername != username || issuer != session.SessionManagerClaimsIssuer {
log.Printf("Claims: %+v", ctx.Value("claims")) // this line for debug
if err := s.enf.EnforceErr(ctx.Value("claims"), rbacpolicy.ResourceAccounts, rbacpolicy.ActionUpdate, q.Name); err != nil {
return nil, fmt.Errorf("permission denied: %w", err)
}
Expand Down
47 changes: 34 additions & 13 deletions server/account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,30 +82,51 @@ func getAdminAccount(mgr *settings.SettingsManager) (*settings.Account, error) {
}

func adminContext(ctx context.Context) context.Context {
// nolint:staticcheck
return context.WithValue(ctx, "claims", &jwt.RegisteredClaims{Subject: "admin", Issuer: sessionutil.SessionManagerClaimsIssuer})
claims := jwt.MapClaims{
"sub": "admin",
"iss": sessionutil.SessionManagerClaimsIssuer,
"groups": []string{"role:admin"},
"federated_claims": map[string]interface{}{
"user_id": "admin",
},
}
ctx = context.WithValue(ctx, sessionutil.ClaimsKey(), claims)
ctx = context.WithValue(ctx, "claims", claims)
return ctx
}

func ssoAdminContext(ctx context.Context, iat time.Time) context.Context {
// nolint:staticcheck
return context.WithValue(ctx, "claims", &jwt.RegisteredClaims{
Subject: "admin",
Issuer: "https://myargocdhost.com/api/dex",
IssuedAt: jwt.NewNumericDate(iat),
})
claims := jwt.MapClaims{
"sub": "admin",
"iss": "https://myargocdhost.com/api/dex",
"iat": jwt.NewNumericDate(iat),
"groups": []string{"role:admin"}, // Add admin group
"federated_claims": map[string]interface{}{
"user_id": "admin",
},
}
// Set both context values
ctx = context.WithValue(ctx, sessionutil.ClaimsKey(), claims)
ctx = context.WithValue(ctx, "claims", claims)

return ctx
}

func projTokenContext(ctx context.Context) context.Context {
// nolint:staticcheck
return context.WithValue(ctx, "claims", &jwt.RegisteredClaims{
Subject: "proj:demo:deployer",
Issuer: sessionutil.SessionManagerClaimsIssuer,
})
claims := jwt.MapClaims{
"sub": "proj:demo:deployer",
"iss": sessionutil.SessionManagerClaimsIssuer,
"groups": []string{"proj:demo:deployer"},
}
ctx = context.WithValue(ctx, sessionutil.ClaimsKey(), claims)
ctx = context.WithValue(ctx, "claims", claims)
return ctx
}

func TestUpdatePassword(t *testing.T) {
accountServer, sessionServer := newTestAccountServer(context.Background())
ctx := adminContext(context.Background())

var err error

// ensure password is not allowed to be updated if given bad password
Expand Down
3 changes: 0 additions & 3 deletions util/oidc/oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,6 @@ func TestGetUserInfo(t *testing.T) {
},
"groups": []interface{}{"githubOrg:engineers"},
}
// json.NewEncoder(w).Encode(response)
if err := json.NewEncoder(w).Encode(response); err != nil {
t.Errorf("failed to encode response: %v", err)
}
Expand Down Expand Up @@ -910,7 +909,6 @@ func TestGetUserInfo(t *testing.T) {
"sub": "sub-only-user",
"groups": []interface{}{"githubOrg:engineers"},
}
// json.NewEncoder(w).Encode(response)
if err := json.NewEncoder(w).Encode(response); err != nil {
t.Errorf("failed to encode response: %v", err)
}
Expand Down Expand Up @@ -970,7 +968,6 @@ func TestGetUserInfo(t *testing.T) {
},
"groups": []interface{}{"githubOrg:engineers"},
}
// json.NewEncoder(w).Encode(response)
if err := json.NewEncoder(w).Encode(response); err != nil {
t.Errorf("failed to encode response: %v", err)
}
Expand Down
22 changes: 15 additions & 7 deletions util/session/sessionmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,19 @@ func (mgr *SessionManager) Create(subject string, secondsBeforeExpiry int64, id
// Create a new token object, specifying signing method and the claims
// you would like it to contain.
now := time.Now().UTC()
claims := jwt.RegisteredClaims{
IssuedAt: jwt.NewNumericDate(now),
Issuer: SessionManagerClaimsIssuer,
NotBefore: jwt.NewNumericDate(now),
Subject: subject,
ID: id,
claims := jwt.MapClaims{
"iat": now.Unix(),
"iss": SessionManagerClaimsIssuer,
"nbf": now.Unix(),
"sub": subject,
"jti": id,
"federated_claims": map[string]interface{}{
"user_id": "", // Empty for local auth
},
}
if secondsBeforeExpiry > 0 {
expires := now.Add(time.Duration(secondsBeforeExpiry) * time.Second)
claims.ExpiresAt = jwt.NewNumericDate(expires)
claims["exp"] = expires.Unix()
}

return mgr.signClaims(claims)
Expand Down Expand Up @@ -641,6 +644,11 @@ type contextKey struct{}

var claimsKey = contextKey{}

// ClaimsKey returns the context key used for claims
func ClaimsKey() interface{} {
return claimsKey
}

func mapClaims(ctx context.Context) (jwt.MapClaims, bool) {
claims, ok := ctx.Value(claimsKey).(jwt.Claims)
if !ok {
Expand Down

0 comments on commit b344eae

Please sign in to comment.