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
16 changes: 8 additions & 8 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ License URL: https://github.com/clipperhouse/uax29/blob/v2.3.0/LICENSE

----------
Module: github.com/codesphere-cloud/cs-go/pkg/io
Version: v0.14.1
Version: v0.15.0
License: Apache-2.0
License URL: https://github.com/codesphere-cloud/cs-go/blob/v0.14.1/LICENSE
License URL: https://github.com/codesphere-cloud/cs-go/blob/v0.15.0/LICENSE

----------
Module: github.com/codesphere-cloud/oms/internal/tmpl
Expand Down Expand Up @@ -77,9 +77,9 @@ License URL: https://github.com/inconshreveable/go-update/blob/8152e7eb6ccf/inte

----------
Module: github.com/jedib0t/go-pretty/v6
Version: v6.7.5
Version: v6.7.7
License: MIT
License URL: https://github.com/jedib0t/go-pretty/blob/v6.7.5/LICENSE
License URL: https://github.com/jedib0t/go-pretty/blob/v6.7.7/LICENSE

----------
Module: github.com/mattn/go-runewidth
Expand Down Expand Up @@ -155,9 +155,9 @@ License URL: https://github.com/yaml/go-yaml/blob/v3.0.4/LICENSE

----------
Module: golang.org/x/crypto
Version: v0.45.0
Version: v0.46.0
License: BSD-3-Clause
License URL: https://cs.opensource.google/go/x/crypto/+/v0.45.0:LICENSE
License URL: https://cs.opensource.google/go/x/crypto/+/v0.46.0:LICENSE

----------
Module: golang.org/x/oauth2
Expand All @@ -167,9 +167,9 @@ License URL: https://cs.opensource.google/go/x/oauth2/+/v0.33.0:LICENSE

----------
Module: golang.org/x/text
Version: v0.31.0
Version: v0.32.0
License: BSD-3-Clause
License URL: https://cs.opensource.google/go/x/text/+/v0.31.0:LICENSE
License URL: https://cs.opensource.google/go/x/text/+/v0.32.0:LICENSE

----------
Module: gopkg.in/yaml.v3
Expand Down
46 changes: 28 additions & 18 deletions cli/cmd/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import (
"github.com/spf13/cobra"
)

const (
API_KEY_ROLE_ADMIN = "Admin"
API_KEY_ROLE_DEV = "Dev"
API_KEY_ROLE_EXT = "Ext"
)

type RegisterCmd struct {
cmd *cobra.Command
Opts RegisterOpts
Expand Down Expand Up @@ -39,24 +45,6 @@ func (c *RegisterCmd) RunE(_ *cobra.Command, args []string) error {
return nil
}

func (c *RegisterCmd) Register(p portal.Portal) (*portal.ApiKey, error) {
var err error
var expiresAt time.Time
if c.Opts.ExpiresAt != "" {
expiresAt, err = time.Parse(time.RFC3339, c.Opts.ExpiresAt)
if err != nil {
return nil, fmt.Errorf("failed to parse expiration date: %w", err)
}
}

newKey, err := p.RegisterAPIKey(c.Opts.Owner, c.Opts.Organization, c.Opts.Role, expiresAt)
if err != nil {
return nil, fmt.Errorf("failed to register API key: %w", err)
}

return newKey, nil
}

func AddRegisterCmd(list *cobra.Command, opts *GlobalOptions) {
c := RegisterCmd{
cmd: &cobra.Command{
Expand All @@ -75,3 +63,25 @@ func AddRegisterCmd(list *cobra.Command, opts *GlobalOptions) {

list.AddCommand(c.cmd)
}

func (c *RegisterCmd) Register(p portal.Portal) (*portal.ApiKey, error) {
if c.Opts.Role != API_KEY_ROLE_ADMIN && c.Opts.Role != API_KEY_ROLE_DEV && c.Opts.Role != API_KEY_ROLE_EXT {
return nil, fmt.Errorf("invalid role: %s. Available roles are: Admin, Dev, Ext", c.Opts.Role)
}

var err error
var expiresAt time.Time
if c.Opts.ExpiresAt != "" {
expiresAt, err = time.Parse(time.RFC3339, c.Opts.ExpiresAt)
if err != nil {
return nil, fmt.Errorf("failed to parse expiration date: %w", err)
}
}

newKey, err := p.RegisterAPIKey(c.Opts.Owner, c.Opts.Organization, c.Opts.Role, expiresAt)
if err != nil {
return nil, fmt.Errorf("failed to register API key: %w", err)
}

return newKey, nil
}
59 changes: 53 additions & 6 deletions cli/cmd/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var _ = Describe("RegisterCmd", func() {
expiresAt = "2025-05-01T15:04:05Z"
owner = "test-owner"
organization = "test-org"
role = "admin"
role = cmd.API_KEY_ROLE_ADMIN
c = cmd.RegisterCmd{
Opts: cmd.RegisterOpts{
Owner: owner,
Expand All @@ -43,15 +43,17 @@ var _ = Describe("RegisterCmd", func() {

Context("when expiration date is valid", func() {
It("registers the API key successfully", func() {
parsedTime, _ := time.Parse(time.RFC3339, expiresAt)
parsedTime, err := time.Parse(time.RFC3339, expiresAt)
Expect(err).To(BeNil())
mockPortal.EXPECT().RegisterAPIKey(owner, organization, role, parsedTime).Return(&portal.ApiKey{}, nil)
ak, err := c.Register(mockPortal)
Expect(err).To(BeNil())
Expect(ak).NotTo(BeNil())
})

It("returns error if Register fails", func() {
parsedTime, _ := time.Parse(time.RFC3339, expiresAt)
parsedTime, err := time.Parse(time.RFC3339, expiresAt)
Expect(err).To(BeNil())
mockPortal.EXPECT().RegisterAPIKey(owner, organization, role, parsedTime).Return((*portal.ApiKey)(nil), fmt.Errorf("some error"))
ak, err := c.Register(mockPortal)
Expect(ak).To(BeNil())
Expand All @@ -60,15 +62,60 @@ var _ = Describe("RegisterCmd", func() {
})

Context("when expiration date is invalid", func() {
BeforeEach(func() {
c.Opts.ExpiresAt = "invalid-date"
})
It("returns error for invalid expiration date", func() {
c.Opts.ExpiresAt = "invalid-date"
ak, err := c.Register(mockPortal)
Expect(ak).To(BeNil())
Expect(err).To(MatchError(ContainSubstring("failed to parse expiration date")))
})
})

Context("when role is valid", func() {
It("accepts Admin role", func() {
c.Opts.Role = cmd.API_KEY_ROLE_ADMIN
parsedTime, err := time.Parse(time.RFC3339, expiresAt)
Expect(err).To(BeNil())

mockPortal.EXPECT().RegisterAPIKey(owner, organization, cmd.API_KEY_ROLE_ADMIN, parsedTime).Return(&portal.ApiKey{}, nil)

ak, err := c.Register(mockPortal)
Expect(err).To(BeNil())
Expect(ak).NotTo(BeNil())
})

It("accepts Dev role", func() {
c.Opts.Role = cmd.API_KEY_ROLE_DEV
parsedTime, err := time.Parse(time.RFC3339, expiresAt)
Expect(err).To(BeNil())

mockPortal.EXPECT().RegisterAPIKey(owner, organization, cmd.API_KEY_ROLE_DEV, parsedTime).Return(&portal.ApiKey{}, nil)

ak, err := c.Register(mockPortal)
Expect(err).To(BeNil())
Expect(ak).NotTo(BeNil())
})

It("accepts Ext role", func() {
c.Opts.Role = cmd.API_KEY_ROLE_EXT
parsedTime, err := time.Parse(time.RFC3339, expiresAt)
Expect(err).To(BeNil())

mockPortal.EXPECT().RegisterAPIKey(owner, organization, cmd.API_KEY_ROLE_EXT, parsedTime).Return(&portal.ApiKey{}, nil)

ak, err := c.Register(mockPortal)
Expect(err).To(BeNil())
Expect(ak).NotTo(BeNil())
})
})

Context("when role is invalid", func() {
It("returns error for invalid role", func() {
c.Opts.Role = "InvalidRole"
ak, err := c.Register(mockPortal)
Expect(ak).To(BeNil())
Expect(err).To(MatchError(ContainSubstring("invalid role: InvalidRole")))
})
})
})

var _ = Describe("AddRegisterCmd", func() {
Expand Down
16 changes: 8 additions & 8 deletions internal/tmpl/NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ License URL: https://github.com/clipperhouse/uax29/blob/v2.3.0/LICENSE

----------
Module: github.com/codesphere-cloud/cs-go/pkg/io
Version: v0.14.1
Version: v0.15.0
License: Apache-2.0
License URL: https://github.com/codesphere-cloud/cs-go/blob/v0.14.1/LICENSE
License URL: https://github.com/codesphere-cloud/cs-go/blob/v0.15.0/LICENSE

----------
Module: github.com/codesphere-cloud/oms/internal/tmpl
Expand Down Expand Up @@ -77,9 +77,9 @@ License URL: https://github.com/inconshreveable/go-update/blob/8152e7eb6ccf/inte

----------
Module: github.com/jedib0t/go-pretty/v6
Version: v6.7.5
Version: v6.7.7
License: MIT
License URL: https://github.com/jedib0t/go-pretty/blob/v6.7.5/LICENSE
License URL: https://github.com/jedib0t/go-pretty/blob/v6.7.7/LICENSE

----------
Module: github.com/mattn/go-runewidth
Expand Down Expand Up @@ -155,9 +155,9 @@ License URL: https://github.com/yaml/go-yaml/blob/v3.0.4/LICENSE

----------
Module: golang.org/x/crypto
Version: v0.45.0
Version: v0.46.0
License: BSD-3-Clause
License URL: https://cs.opensource.google/go/x/crypto/+/v0.45.0:LICENSE
License URL: https://cs.opensource.google/go/x/crypto/+/v0.46.0:LICENSE

----------
Module: golang.org/x/oauth2
Expand All @@ -167,9 +167,9 @@ License URL: https://cs.opensource.google/go/x/oauth2/+/v0.33.0:LICENSE

----------
Module: golang.org/x/text
Version: v0.31.0
Version: v0.32.0
License: BSD-3-Clause
License URL: https://cs.opensource.google/go/x/text/+/v0.31.0:LICENSE
License URL: https://cs.opensource.google/go/x/text/+/v0.32.0:LICENSE

----------
Module: gopkg.in/yaml.v3
Expand Down