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

Return cloud-credentials with empty attribute #1333

Merged
merged 7 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion internal/jujuapi/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,11 @@ func getIdentityCredentials(ctx context.Context, user *openfga.User, j JIMM, arg
var err error
content.Attributes, _, err = j.GetCloudCredentialAttributes(ctx, user, c, args.IncludeSecrets)
Copy link
Contributor

@SimoneDutto SimoneDutto Aug 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thinking out loud...

now this map is set to nil, and if you try to access it it will panic, i would prefer j.GetCloudCredentialAttributes to return an empty map without any error. It is even more correct, because we don't want to treat empty attributes as an error.
What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes fair point

if err != nil {
return nil, errors.E(err)
if errors.ErrorCode(err) == errors.CodeNotFound {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah true actually! Good point

Copy link
Contributor Author

@kian99 kian99 Aug 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated this, seems like a nil map is treated like a nil slice, in the sense that using a nil map e.g. range myMap won't cause a panic even though it is nil.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah good point, fixed.

content.Attributes = map[string]string{}
} else {
return nil, errors.E(err)
}
}
mas := make([]jujuparams.ModelAccess, len(c.Models))
for i, m := range c.Models {
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
Loading