Skip to content
Merged
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
42 changes: 8 additions & 34 deletions cli/cmd/revoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,23 @@
package cmd

import (
"fmt"

"github.com/codesphere-cloud/cs-go/pkg/io"
"github.com/codesphere-cloud/oms/internal/portal"
"github.com/spf13/cobra"
)

type RevokeCmd struct {
cmd *cobra.Command
Opts RevokeOpts
}

type RevokeOpts struct {
GlobalOptions
Key string
cmd *cobra.Command
}

func (c *RevokeCmd) RunE(_ *cobra.Command, args []string) error {
p := portal.NewPortalClient()
return c.Revoke(p)
}

func (c *RevokeCmd) Revoke(p portal.Portal) error {
err := p.RevokeAPIKey(c.Opts.Key)
if err != nil {
return fmt.Errorf("failed to revoke API key: %w", err)
}

return nil
}

func AddRevokeCmd(list *cobra.Command, opts GlobalOptions) {
c := RevokeCmd{
func AddRevokeCmd(rootCmd *cobra.Command, opts GlobalOptions) {
revoke := RevokeCmd{
cmd: &cobra.Command{
Use: "revoke",
Short: "Revoke an API key",
Long: io.Long(`Revoke an OMS portal API key.`),
Short: "Revoke resources available through OMS",
Long: io.Long(`Revoke resources managed by or available for OMS,
eg. api keys.`),
},
Opts: RevokeOpts{GlobalOptions: opts},
}
c.cmd.Flags().StringVarP(&c.Opts.Key, "key", "k", "", "API key to revoke")

c.cmd.RunE = c.RunE

list.AddCommand(c.cmd)
rootCmd.AddCommand(revoke.cmd)
AddRevokeAPIKeyCmd(revoke.cmd, opts)
}
55 changes: 55 additions & 0 deletions cli/cmd/revoke_api_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"fmt"

"github.com/codesphere-cloud/cs-go/pkg/io"
"github.com/codesphere-cloud/oms/internal/portal"
"github.com/codesphere-cloud/oms/internal/util"
"github.com/spf13/cobra"
)

type RevokeAPIKeyCmd struct {
cmd *cobra.Command
Opts RevokeAPIKeyOpts
}

type RevokeAPIKeyOpts struct {
GlobalOptions
ID string
}

func (c *RevokeAPIKeyCmd) RunE(_ *cobra.Command, args []string) error {
p := portal.NewPortalClient()
return c.Revoke(p)
}

func (c *RevokeAPIKeyCmd) Revoke(p portal.Portal) error {
err := p.RevokeAPIKey(c.Opts.ID)
if err != nil {
return fmt.Errorf("failed to revoke API key: %w", err)
}

return nil
}

func AddRevokeAPIKeyCmd(list *cobra.Command, opts GlobalOptions) {
c := RevokeAPIKeyCmd{
cmd: &cobra.Command{
Use: "api-key",
Short: "Revoke an API key",
Long: io.Long(`Revoke an OMS portal API key.`),
},
Opts: RevokeAPIKeyOpts{GlobalOptions: opts},
}
c.cmd.Flags().StringVarP(&c.Opts.ID, "id", "i", "", "API key id to revoke")

util.MarkFlagRequired(c.cmd, "id")

c.cmd.RunE = c.RunE

list.AddCommand(c.cmd)
}
16 changes: 8 additions & 8 deletions cli/cmd/revoke_test.go → cli/cmd/revoke_api_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ import (
var _ = Describe("RevokeCmd", func() {
var (
mockPortal *portal.MockPortal
c cmd.RevokeCmd
c cmd.RevokeAPIKeyCmd
key string
)

BeforeEach(func() {
mockPortal = portal.NewMockPortal(GinkgoT())
key = "test-key"
c = cmd.RevokeCmd{
Opts: cmd.RevokeOpts{
Key: key,
c = cmd.RevokeAPIKeyCmd{
Opts: cmd.RevokeAPIKeyOpts{
ID: key,
},
}
})
Expand All @@ -48,14 +48,14 @@ var _ = Describe("RevokeCmd", func() {
})
})

var _ = Describe("AddRevokeCmd", func() {
It("adds the revoke command to the parent", func() {
var _ = Describe("AddRevokeAPIKeyCmd", func() {
It("adds the api-key command to the parent", func() {
parent := &cobra.Command{}
opts := cmd.GlobalOptions{}
cmd.AddRevokeCmd(parent, opts)
cmd.AddRevokeAPIKeyCmd(parent, opts)
found := false
for _, c := range parent.Commands() {
if c.Use == "revoke" {
if c.Use == "api-key" {
found = true
break
}
Expand Down
6 changes: 3 additions & 3 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ func GetRootCmd() *cobra.Command {
AddUpdateCmd(rootCmd)
AddListCmd(rootCmd, opts)
AddDownloadCmd(rootCmd, opts)
// OMS API key management commands

// OMS API key management commands
AddRegisterCmd(rootCmd, opts)
AddRevokeCmd(rootCmd, opts)

return rootCmd
}

Expand Down
13 changes: 13 additions & 0 deletions internal/portal/api_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package portal

import "time"

type ApiKey struct {
RID string `json:"rid"`
ApiKey string `json:"api_key"`
Owner string `json:"owner"`
Organization string `json:"organization"`
Role string `json:"role"`
CreatedAt time.Time `json:"createdAt"`
ExpiresAt time.Time `json:"expiresAt"`
}
17 changes: 10 additions & 7 deletions internal/portal/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,21 +222,23 @@ func (c *PortalClient) RegisterAPIKey(owner string, organization string, role st
}
defer func() { _ = resp.Body.Close() }()

var newKey string
err = json.NewDecoder(resp.Body).Decode(&newKey)
newKey := &ApiKey{}
err = json.NewDecoder(resp.Body).Decode(newKey)
if err != nil {
return fmt.Errorf("failed to decode response body: %w", err)
}

fmt.Printf("API key for owner %s registered successfully: %s\n", owner, newKey)
fmt.Println("API key registered successfully!")
fmt.Printf("Owner: %s\nOrganisation: %s\nKey: %s\n", newKey.Owner, newKey.Organization, newKey.ApiKey)

return nil
}

func (c *PortalClient) RevokeAPIKey(key string) error {
func (c *PortalClient) RevokeAPIKey(keyId string) error {
req := struct {
Key string `json:"key"`
KeyID string `json:"key_id"`
}{
Key: key,
KeyID: keyId,
}

reqBody, err := json.Marshal(req)
Expand All @@ -250,6 +252,7 @@ func (c *PortalClient) RevokeAPIKey(key string) error {
}
defer func() { _ = resp.Body.Close() }()

fmt.Println("API key revoked successfully")
fmt.Println("API key revoked successfully!")

return nil
}
14 changes: 14 additions & 0 deletions internal/util/required_flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package util

import (
"fmt"

"github.com/spf13/cobra"
)

func MarkFlagRequired(cmd *cobra.Command, name string) {
err := cmd.MarkFlagRequired(name)
if err != nil {
panic(fmt.Errorf("failed to mark flag as required, please check existence: %w", err))
}
}