Skip to content

Commit

Permalink
add restore secret command
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelignat committed Apr 17, 2024
1 parent e5ae030 commit af51b1a
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func New(out io.Writer, in io.Reader, args []string) *cobra.Command {
envs.New(),
configs.New("Compute", queries.ConfigClassCompute),
configs.New("Storage", queries.ConfigClassStorage),
configs.New("Integration", queries.ConfigClassNotification, queries.ConfigClassQueue),
configs.New("Integration", queries.ConfigClassNotification),
containerrepository.New(),
secrets.New(),
keys.New(),
Expand Down
2 changes: 1 addition & 1 deletion cmd/secrets/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type DeleteConfig struct {
func DeleteCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "delete [compute] [env] [code]",
Short: "Will delete a container repository for a given compute",
Short: "Will delete a secret for a given compute and environment",
Args: cobra.ExactArgs(3),
PreRun: util.BindPreRun,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
76 changes: 76 additions & 0 deletions cmd/secrets/restore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package secrets

import (
"context"

"github.com/getnoops/ops/pkg/config"
"github.com/getnoops/ops/pkg/queries"
"github.com/getnoops/ops/pkg/util"
"github.com/google/uuid"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

type RestoreConfig struct {
}

func RestoreCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "restore [compute] [env] [code]",
Short: "Will restore a secret for a given compute and environment. Must be in a pending deletion state.",
Args: cobra.ExactArgs(3),
PreRun: util.BindPreRun,
RunE: func(cmd *cobra.Command, args []string) error {
configCode := args[0]
envCode := args[1]
code := args[2]

ctx := cmd.Context()
return Restore(ctx, configCode, envCode, code)
},
ValidArgs: []string{"compute", "code"},
}
return cmd
}

func Restore(ctx context.Context, computeCode string, environmentCode string, code string) error {
cfg, err := config.New[DeleteConfig, *uuid.UUID](ctx, viper.GetViper())
if err != nil {
return err
}

q, err := queries.New(ctx, cfg)
if err != nil {
return err
}

organisation, err := q.GetCurrentOrganisation(ctx)
if err == config.ErrNoOrganisation {
cfg.WriteStderr("no organisation set")
return nil
}
if err != nil {
return err
}

config, err := q.GetConfig(ctx, organisation.Id, computeCode)
if err != nil {
cfg.WriteStderr("failed to get configs")
return err
}

secret, err := GetSecret(config.Secrets, environmentCode, code)
if err != nil {
cfg.WriteStderr("secret not found")
return err
}

out, err := q.DeleteSecret(ctx, organisation.Id, secret.Id)
if err != nil {
cfg.WriteStderr("failed to delete container repository")
return err
}

cfg.WriteObject(out)
return nil
}
56 changes: 55 additions & 1 deletion pkg/queries/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion pkg/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Queries interface {

CreateSecret(ctx context.Context, organisationId uuid.UUID, id uuid.UUID, configId uuid.UUID, environmentId uuid.UUID, code string, value string) (*uuid.UUID, error)
UpdateSecret(ctx context.Context, organisationId uuid.UUID, id uuid.UUID, configId uuid.UUID, environmentId uuid.UUID, code string, value string) (*uuid.UUID, error)
RestoreSecret(ctx context.Context, organisationId uuid.UUID, id uuid.UUID) (*uuid.UUID, error)
DeleteSecret(ctx context.Context, organisationId uuid.UUID, id uuid.UUID) (*uuid.UUID, error)

GetApiKeys(ctx context.Context, organisationId uuid.UUID, page int, pageSize int) (*GetApiKeysApiKeysPagedApiKeysOutput, error)
Expand Down Expand Up @@ -158,11 +159,20 @@ func (q *queries) CreateSecret(ctx context.Context, organisationId uuid.UUID, id
func (q *queries) UpdateSecret(ctx context.Context, organisationId uuid.UUID, id uuid.UUID, configId uuid.UUID, environmentId uuid.UUID, code string, value string) (*uuid.UUID, error) {
resp, err := UpdateSecret(ctx, q.client, organisationId, id, configId, environmentId, code, value)
if err != nil {
return nil, err
return nil, fmt.Errorf("UpdateSecret unexpected response: %v", err)
}
return &resp.UpdateSecret, nil
}

func (q *queries) RestoreSecret(ctx context.Context, organisationId uuid.UUID, id uuid.UUID) (*uuid.UUID, error) {
resp, err := RestoreSecret(ctx, q.client, organisationId, id)
if err != nil {
return nil, fmt.Errorf("RestoreSecret unexpected response: %v", err)

}
return &resp.RestoreSecret, nil
}

func (q *queries) DeleteSecret(ctx context.Context, organisationId uuid.UUID, id uuid.UUID) (*uuid.UUID, error) {
resp, err := DeleteSecret(ctx, q.client, organisationId, id)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions pkg/queries/query.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,13 @@ mutation UpdateSecret($organisationId: UUID!, $aggregateId: UUID!, $configId: UU
})
}

mutation RestoreSecret($organisationId: UUID!, $id: UUID!) {
restoreSecret(input: {
organisation_id: $organisationId,
id: $id,
})
}

mutation DeleteSecret($organisationId: UUID!, $id: UUID!) {
deleteSecret(input: {
organisation_id: $organisationId,
Expand Down
12 changes: 7 additions & 5 deletions pkg/queries/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ input ConfigAccessInput {
enum ConfigClass {
compute
storage
queue
notification
}

Expand Down Expand Up @@ -557,6 +556,7 @@ type Mutation {
createOrganisation(input: CreateOrganisationInput!): UUID!
updateOrganisation(input: UpdateOrganisationInput!): UUID!
deleteOrganisation(input: IdInput!): UUID!
leaveOrganisation(input: IdInput!): UUID!
updateUser(input: UpdateUserInput!): UUID!
createMember(input: CreateMemberInput!): UUID!
deleteMember(input: IdInput!): UUID!
Expand Down Expand Up @@ -591,6 +591,7 @@ type Mutation {
loginContainerRepository(input: IdInput!): AuthContainerRepository!
createSecret(input: CreateSecretInput!): UUID!
updateSecret(input: CreateSecretInput!): UUID!
restoreSecret(input: IdWithOrgInput!): UUID!
deleteSecret(input: IdWithOrgInput!): UUID!
createApiKey(input: CreateApiKeyInput!): IdWithToken!
updateApiKey(input: UpdateApiKeyInput!): IdWithToken!
Expand Down Expand Up @@ -820,13 +821,13 @@ enum PlanTier {
}

type PlanUnits {
microservices: Int!
databases: Int!
compute: Int!
storage: Int!
}

input PlanUnitsInput {
microservices: Int!
databases: Int!
compute: Int!
storage: Int!
}

input PreviewProrationInput {
Expand Down Expand Up @@ -972,6 +973,7 @@ type Secret {
code: String!
created_at: Time!
updated_at: Time
deletion_date: Time
}

input SecretsInput {
Expand Down

0 comments on commit af51b1a

Please sign in to comment.