Skip to content
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

Update token scopes even when bindings aren't being changed #90

Merged
merged 1 commit into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions plugin/path_role_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,9 @@ func (b *backend) pathRoleSetCreateUpdate(ctx context.Context, req *logical.Requ
// If no new bindings or new bindings are exactly same as old bindings,
// just update the role set without rotating service account.
if !newBindings || rs.bindingHash() == getStringHash(bRaw.(string)) {
if rs.TokenGen != nil {
rs.TokenGen.Scopes = scopes
}
// Just save role with updated metadata:
if err := rs.save(ctx, req.Storage); err != nil {
return logical.ErrorResponse(err.Error()), nil
Expand Down
86 changes: 86 additions & 0 deletions plugin/path_role_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,92 @@ func TestPathRoleSet_RotateKeyRoleSet(t *testing.T) {
verifyProjectBindingsRemoved(t, td, newSa.Email, roles)
}

func TestPathRoleSet_UpdateTokenRoleSetScopes(t *testing.T) {
rsName := "test-updatetokenrsscopes"
roles := util.StringSet{
"roles/viewer": struct{}{},
}

// Initial test set up - backend, initial config, test resources in project
td := setupTest(t, "0s", "2h")
defer cleanup(t, td, rsName, roles)

projRes := fmt.Sprintf(testProjectResourceTemplate, td.Project)

// Create role set
expectedBinds := ResourceBindings{projRes: roles}
bindsRaw, err := util.BindingsHCL(expectedBinds)
if err != nil {
t.Fatalf("unable to convert resource bindings to HCL string: %v", err)
}
testRoleSetCreate(t, td, rsName,
map[string]interface{}{
"project": td.Project,
"secret_type": SecretTypeAccessToken,
"bindings": bindsRaw,
"token_scopes": []string{"https://www.googleapis.com/auth/cloud-platform"},
})

// Verify
respData := testRoleSetRead(t, td, rsName)
if respData == nil {
t.Fatalf("expected role set to have been created")
}
verifyReadData(t, respData, map[string]interface{}{
"secret_type": SecretTypeAccessToken,
"project": td.Project,
"bindings": expectedBinds,
"token_scopes": []string{"https://www.googleapis.com/auth/cloud-platform"},
})

initSa := getServiceAccount(t, td.IamAdmin, respData)
verifyProjectBinding(t, td, initSa.Email, roles)

initK := verifyRoleSetTokenKey(t, td, rsName)
if !strings.HasPrefix(initK.Name, initSa.Name) {
t.Fatalf("expected token key to have been generated under initial service account")
}

// Update role set
testRoleSetUpdate(t, td, rsName,
map[string]interface{}{
"token_scopes": []string{
"https://www.googleapis.com/auth/compute",
"https://www.googleapis.com/auth/compute.readonly",
},
})

// Verify
respData = testRoleSetRead(t, td, rsName)
if respData == nil {
t.Fatalf("expected role set to have been created")
}
verifyReadData(t, respData, map[string]interface{}{
"secret_type": SecretTypeAccessToken,
"project": td.Project,
"bindings": expectedBinds,
"token_scopes": []string{
"https://www.googleapis.com/auth/compute",
"https://www.googleapis.com/auth/compute.readonly",
},
})
newSa := getServiceAccount(t, td.IamAdmin, respData)
verifyProjectBinding(t, td, newSa.Email, roles)
newK := verifyRoleSetTokenKey(t, td, rsName)
if !strings.HasPrefix(newK.Name, newSa.Name) {
t.Fatalf("expected token key to have been generated under same service account")
}

// Verify service account didn't change
if newSa.Name != initSa.Name {
t.Fatalf("expected role set not to have new service account after update")
}

// 4. Delete role set
testRoleSetDelete(t, td, rsName, newSa.Name)
verifyProjectBindingsRemoved(t, td, newSa.Email, roles)
}

func TestPathRoleSet_UpdateTokenRoleSet(t *testing.T) {
rsName := "test-updatetokenrs"
initRoles := util.StringSet{
Expand Down