Skip to content
Open
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: 2 additions & 1 deletion pkg/auth/auth_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ func CheckACLs(cmd *cobra.Command, f *cmdutil.Factory) error {
}
apiKey, err := client.GetApiKey(client.NewApiGetApiKeyRequest(key))
if err != nil {
return err
// Self-read requires its own ACL; don't fail on that, let the real call decide.
return nil
}

var hasAcls []string
Expand Down
31 changes: 30 additions & 1 deletion pkg/auth/auth_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func Test_CheckACLs(t *testing.T) {
cmd *cobra.Command
adminKey bool
ACLs []search.Acl
selfReadFails bool
wantErr bool
wantErrMessage string
}{
Expand Down Expand Up @@ -132,6 +133,29 @@ See https://www.algolia.com/doc/guides/security/api-keys/#rights-and-restriction
ACLs: []search.Acl{search.ACL_SEARCH},
wantErr: false,
},
{
name: "self-read fails, need non-admin ACLs",
cmd: &cobra.Command{
Annotations: map[string]string{
"acls": "settings",
},
},
adminKey: false,
selfReadFails: true,
wantErr: false,
},
{
name: "self-read fails, need admin key",
cmd: &cobra.Command{
Annotations: map[string]string{
"acls": "admin",
},
},
adminKey: false,
selfReadFails: true,
wantErr: true,
wantErrMessage: "this command requires an admin API key. Use the `--api-key` flag with a valid admin API key",
},
}

for _, tt := range tests {
Expand All @@ -149,7 +173,12 @@ See https://www.algolia.com/doc/guides/security/api-keys/#rights-and-restriction
)
}

if tt.ACLs != nil && !tt.adminKey {
if tt.selfReadFails && !tt.adminKey {
r.Register(
httpmock.REST("GET", "1/keys/test"),
httpmock.ErrorResponse(),
)
} else if tt.ACLs != nil && !tt.adminKey {
r.Register(
httpmock.REST("GET", "1/keys/test"),
httpmock.JSONResponse(search.ApiKey{Acl: tt.ACLs}),
Expand Down