Skip to content

Commit

Permalink
Return cloud-credentials with empty attribute (canonical#1333)
Browse files Browse the repository at this point in the history
* return cloud-credentials with empty attribute

* set empty map if attributes not found

* change application logic to not return error on empty attributes

* add app layer test

* return empty map rather than nil

* fix test
  • Loading branch information
kian99 committed Sep 3, 2024
1 parent 7b1395f commit 5e0a960
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
6 changes: 3 additions & 3 deletions internal/jimm/cloudcredential.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@ func (j *JIMM) GetCloudCredentialAttributes(ctx context.Context, user *openfga.U
err = errors.E(op, err)
return
}
if len(attrs) == 0 {
return map[string]string{}, nil, nil
}

if hidden {
return
Expand Down Expand Up @@ -377,8 +380,5 @@ func (j *JIMM) getCloudCredentialAttributes(ctx context.Context, cred *dbmodel.C
if err != nil {
return nil, errors.E(op, err)
}
if len(attr) == 0 && cred.AuthType != "empty" {
return nil, errors.E(op, errors.CodeNotFound, "cloud-credential attributes not found")
}
return attr, nil
}
23 changes: 21 additions & 2 deletions internal/jimm/cloudcredential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package jimm_test
import (
"context"
"database/sql"
"fmt"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -1538,6 +1539,10 @@ cloud-credentials:
client-id: 1234
private-key: super-secret
project-id: 5678
- name: cred-2
cloud: test-cloud
owner: [email protected]
auth-type: certificate
users:
- username: [email protected]
controller-access: superuser
Expand All @@ -1549,6 +1554,7 @@ var getCloudCredentialAttributesTests = []struct {
username string
hidden bool
jimmAdmin bool
cred string
expectAttributes map[string]string
expectRedacted []string
expectError string
Expand All @@ -1557,16 +1563,25 @@ var getCloudCredentialAttributesTests = []struct {
name: "OwnerNoHidden",
username: "[email protected]",
jimmAdmin: true,
cred: "cred-1",
expectAttributes: map[string]string{
"client-email": "[email protected]",
"client-id": "1234",
"project-id": "5678",
},
expectRedacted: []string{"private-key"},
}, {
name: "OwnerNoAttributes",
username: "[email protected]",
jimmAdmin: true,
cred: "cred-2",
expectAttributes: map[string]string{},
expectRedacted: nil,
}, {
name: "OwnerWithHidden",
username: "[email protected]",
hidden: true,
cred: "cred-1",
expectAttributes: map[string]string{
"client-email": "[email protected]",
"client-id": "1234",
Expand All @@ -1577,6 +1592,7 @@ var getCloudCredentialAttributesTests = []struct {
name: "SuperUserNoHidden",
username: "[email protected]",
jimmAdmin: true,
cred: "cred-1",
expectAttributes: map[string]string{
"client-email": "[email protected]",
"client-id": "1234",
Expand All @@ -1588,11 +1604,13 @@ var getCloudCredentialAttributesTests = []struct {
username: "[email protected]",
hidden: true,
jimmAdmin: true,
cred: "cred-1",
expectError: `unauthorized`,
expectErrorCode: errors.CodeUnauthorized,
}, {
name: "OtherUserUnauthorized",
username: "[email protected]",
cred: "cred-1",
expectError: `unauthorized`,
expectErrorCode: errors.CodeUnauthorized,
}}
Expand Down Expand Up @@ -1623,7 +1641,8 @@ func TestGetCloudCredentialAttributes(t *testing.T) {
env.PopulateDBAndPermissions(c, j.ResourceTag(), j.Database, client)
u := env.User("[email protected]").DBObject(c, j.Database)
userBob := openfga.NewUser(&u, client)
cred, err := j.GetCloudCredential(ctx, userBob, names.NewCloudCredentialTag("test-cloud/[email protected]/cred-1"))
credTag := fmt.Sprintf("test-cloud/[email protected]/%s", test.cred)
cred, err := j.GetCloudCredential(ctx, userBob, names.NewCloudCredentialTag(credTag))
c.Assert(err, qt.IsNil)

u = env.User(test.username).DBObject(c, j.Database)
Expand Down Expand Up @@ -1714,7 +1733,7 @@ func TestCloudCredentialAttributeStore(t *testing.T) {

// Update to an "empty" credential
args.Credential.AuthType = "empty"
args.Credential.Attributes = nil
args.Credential.Attributes = map[string]string{}
_, err = j.UpdateCloudCredential(ctx, user, args)
c.Assert(err, qt.IsNil)

Expand Down
27 changes: 27 additions & 0 deletions internal/jujuapi/cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,33 @@ func (s *cloudSuite) TestCredentialContents(c *gc.C) {
}})
}

func (s *cloudSuite) TestCredentialContentsWithEmptyAttributes(c *gc.C) {
conn := s.open(c, nil, "test")
defer conn.Close()
client := cloudapi.NewClient(conn)
credentialTag := names.NewCloudCredentialTag(jimmtest.TestCloudName + "/[email protected]/cred3")
err := client.AddCredential(
credentialTag.String(),
cloud.NewCredential(
"certificate",
nil,
),
)
c.Assert(err, gc.Equals, nil)
creds, err := client.CredentialContents(jimmtest.TestCloudName, "cred3", false)
c.Assert(err, gc.Equals, nil)
c.Assert(creds, jc.DeepEquals, []jujuparams.CredentialContentResult{{
Result: &jujuparams.ControllerCredentialInfo{
Content: jujuparams.CredentialContent{
Name: "cred3",
Cloud: jimmtest.TestCloudName,
AuthType: "certificate",
Attributes: nil,
},
},
}})
}

func (s *cloudSuite) TestRemoveCloud(c *gc.C) {
conn := s.open(c, nil, "test")
defer conn.Close()
Expand Down

0 comments on commit 5e0a960

Please sign in to comment.