Skip to content

Commit

Permalink
Merge pull request #1207 from smallstep/max/admin-check
Browse files Browse the repository at this point in the history
Add IsEnabled method in AdminClient for checking admin API availability
  • Loading branch information
dopey authored Jan 10, 2023
2 parents a78ddc7 + fd921e5 commit 627506b
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions ca/adminClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ type AdminClient struct {
x5cSubject string
}

var ErrAdminAPINotImplemented = errors.New("admin API not implemented")
var ErrAdminAPINotAuthorized = errors.New("admin API not authorized")

// AdminClientError is the client side representation of an
// AdminError returned by the CA.
type AdminClientError struct {
Expand Down Expand Up @@ -137,6 +140,28 @@ func (c *AdminClient) retryOnError(r *http.Response) bool {
return false
}

// IsEnabled checks if the admin API is enabled.
func (c *AdminClient) IsEnabled() error {
u := c.endpoint.ResolveReference(&url.URL{Path: path.Join(adminURLPrefix, "admins")})
resp, err := c.client.Get(u.String())
if err != nil {
return clientError(err)
}
defer resp.Body.Close()

if resp.StatusCode < http.StatusBadRequest {
return nil
}
switch resp.StatusCode {
case http.StatusNotFound, http.StatusNotImplemented:
return ErrAdminAPINotImplemented
case http.StatusUnauthorized:
return ErrAdminAPINotAuthorized
default:
return errors.Errorf("unexpected status code when performing is-enabled check for Admin API: %d", resp.StatusCode)
}
}

// GetAdmin performs the GET /admin/admin/{id} request to the CA.
func (c *AdminClient) GetAdmin(id string) (*linkedca.Admin, error) {
var retried bool
Expand Down

0 comments on commit 627506b

Please sign in to comment.