diff --git a/cli/cmd/register.go b/cli/cmd/register.go index 90754e7f..93750cd1 100644 --- a/cli/cmd/register.go +++ b/cli/cmd/register.go @@ -31,9 +31,13 @@ func (c *RegisterCmd) RunE(_ *cobra.Command, args []string) error { } func (c *RegisterCmd) Register(p portal.Portal) error { - expiresAt, err := time.Parse(time.RFC3339, c.Opts.ExpiresAt) - if err != nil { - return fmt.Errorf("failed to parse expiration date: %w", err) + var err error + var expiresAt time.Time + if c.Opts.ExpiresAt != "" { + expiresAt, err = time.Parse(time.RFC3339, c.Opts.ExpiresAt) + if err != nil { + return fmt.Errorf("failed to parse expiration date: %w", err) + } } err = p.RegisterAPIKey(c.Opts.Owner, c.Opts.Organization, c.Opts.Role, expiresAt) diff --git a/cli/cmd/root.go b/cli/cmd/root.go index b4877659..359b5644 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -29,6 +29,11 @@ func GetRootCmd() *cobra.Command { AddUpdateCmd(rootCmd) AddListCmd(rootCmd, opts) AddDownloadCmd(rootCmd, opts) + + // OMS API key management commands + AddRegisterCmd(rootCmd, opts) + AddRevokeCmd(rootCmd, opts) + return rootCmd } diff --git a/internal/portal/http.go b/internal/portal/http.go index 77842a99..c559d9d4 100644 --- a/internal/portal/http.go +++ b/internal/portal/http.go @@ -86,7 +86,7 @@ func (c *PortalClient) HttpRequest(method string, path string, body []byte) (res fmt.Println("If you already have an API Key, make sure to set it using the environment variable OMS_PORTAL_API_KEY") } var respBody []byte - if resp.StatusCode != http.StatusOK { + if resp.StatusCode >= 300 { if resp.Body != nil { respBody, _ = io.ReadAll(resp.Body) } @@ -216,7 +216,7 @@ func (c *PortalClient) RegisterAPIKey(owner string, organization string, role st return fmt.Errorf("failed to generate request body: %w", err) } - resp, err := c.HttpRequest(http.MethodPost, "/api/key/register", reqBody) + resp, err := c.HttpRequest(http.MethodPost, "/key/register", reqBody) if err != nil { return fmt.Errorf("POST request to register API key failed: %w", err) } @@ -244,7 +244,7 @@ func (c *PortalClient) RevokeAPIKey(key string) error { return fmt.Errorf("failed to generate request body: %w", err) } - resp, err := c.HttpRequest(http.MethodPost, "/api/key/revoke", reqBody) + resp, err := c.HttpRequest(http.MethodPost, "/key/revoke", reqBody) if err != nil { return fmt.Errorf("POST request to revoke API key failed: %w", err) }