Skip to content

Conversation

@arinda-arif
Copy link
Contributor

No description provided.

@arinda-arif arinda-arif marked this pull request as draft December 17, 2021 06:38
@coveralls
Copy link

coveralls commented Dec 17, 2021

Pull Request Test Coverage Report for Build 1744262013

  • 53 of 65 (81.54%) changed or added relevant lines in 2 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage increased (+0.08%) to 71.49%

Changes Missing Coverage Covered Lines Changed/Added Lines %
models/project.go 0 2 0.0%
api/handler/v1beta1/secret.go 53 63 84.13%
Files with Coverage Reduction New Missed Lines %
models/project.go 1 50.0%
Totals Coverage Status
Change from base Build 1739126541: 0.08%
Covered Lines: 5025
Relevant Lines: 7029

💛 - Coveralls

@kushsharma
Copy link
Member

I was thinking what if we have a single set command instead of create/update?
@sravankorumilli @arinda-arif

@arinda-arif
Copy link
Contributor Author

arinda-arif commented Dec 21, 2021

I was thinking what if we have a single set command instead of create/update? @sravankorumilli @arinda-arif

I think having create/update gives benefits of having extra validation, for example:

  • prevent users from replacing existing secrets by mistake when creating
  • prevent creating unnecessary secrets when updating a wrong secret name by mistake

And the flexibility to differentiate who is authorized to create or update in the future.

@kushsharma
Copy link
Member

I was thinking what if we have a single set command instead of create/update? @sravankorumilli @arinda-arif

I think having create/update gives benefits of having extra validation, for example:

  • prevent users from replacing existing secrets by mistake when creating
  • prevent creating unnecessary secrets when updating a wrong secret name by mistake

And the flexibility to differentiate who is authorized to create or update in the future.

Make sense, what if we solve this using a flag --update-only? I am trying to make APIs a little more simpler. What do you think?

@arinda-arif arinda-arif marked this pull request as ready for review January 3, 2022 04:28
@arinda-arif
Copy link
Contributor Author

I was thinking what if we have a single set command instead of create/update? @sravankorumilli @arinda-arif

I think having create/update gives benefits of having extra validation, for example:

  • prevent users from replacing existing secrets by mistake when creating
  • prevent creating unnecessary secrets when updating a wrong secret name by mistake

And the flexibility to differentiate who is authorized to create or update in the future.

Make sense, what if we solve this using a flag --update-only? I am trying to make APIs a little more simpler. What do you think?

Got your point. I assume this is the behavior if we are having that flag:

  • optimus create secret -> will create a secret. if any existing secret is found, will not be updated.
  • optimus create secret --update-only -> will update a secret. if the secret is not found, will not be created.

Does create secret --update-only seems odd? should we use register instead or is there any other suggestion?
cc @sbchaos @sravankorumilli

@sbchaos
Copy link
Contributor

sbchaos commented Jan 3, 2022

Does create secret --update-only seems odd? should we use register instead or is there any other suggestion? cc @sbchaos @sravankorumilli

To me it sounds a bit odd that we are using create to update the value of secret. Register seems like a reasonable alternative to update/create.

@ravisuhag
Copy link
Member

+1 to set as suggested by @kushsharma

@arinda-arif
Copy link
Contributor Author

arinda-arif commented Jan 3, 2022

Agree, let's have set then. For the flag, should we go with --update? having --update-only seems to skip the flag means it can be either create/update, while we actually want to separate both behaviors to avoid the above possible issues.

@sbchaos sbchaos force-pushed the secret-management branch from 0127c4f to 64968f7 Compare January 4, 2022 05:05

## Updating a secret

Updating an already existing secret in Optimus can be done passing the flag update-only to set command:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These examples are misleading, update-only flag is generally used when you explicitly only want to update a secret when it is already there and don't want to create it by mistake. I think we should write it as such.

@sbchaos sbchaos force-pushed the secret-management branch 4 times, most recently from 18d81f1 to 9a8a89b Compare January 4, 2022 11:38
cmd/secret.go Outdated
)

var (
secretTimeout = time.Minute * 15
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is too long of a timeout to recognize the operation has occurred a problem. I guess 2 min should be enough.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will update it to 2 minutes

return nil, status.Errorf(codes.Internal, "%s: failed to update secret %s", err.Error(), req.GetSecretName())
}
} else {
if err := secretRepo.Save(ctx, models.ProjectSecretItem{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't the current implementation of Save will fail this if the secret is already registered? I was thinking the behavior should be

  • if the set is called and it doesn't exist, it will create it
  • if the set is called and it does exist, it will update it
  • if the set is called with --update-only flag and it doesn't exist, it will fail
  • if the set is called with --update-only flag and it does exist, it will update

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current behaviour will update an existing secret at time of create.
If this is not going to be a problem, then I can keep the current behaviour.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry didn't get what you said, can you rephrase it, please? Is the current behavior match with those 4 points?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Existing behaviour (without change on this PR), save will create a new secret, if it exists, it will update the secret.
It might be a problem when the update of existing secret is unintentional at the time of create.

If we encounter this problem very less(unintentional update) then we can retain the existing behaviour.
Otherwise it will be better to be intentional about create or update of secrets.

@sbchaos sbchaos force-pushed the secret-management branch 3 times, most recently from 7d6d278 to c07d01d Compare January 6, 2022 08:58
Co-authored-by: Sandeep Bhardwaj<sandeep.bhardwaj2307@gmail.com>
@sbchaos sbchaos force-pushed the secret-management branch from c07d01d to 52d19d1 Compare January 6, 2022 09:01
}, nil
}

func (sv *RuntimeServiceServer) RegisterSecret(ctx context.Context, req *pb.RegisterSecretRequest) (*pb.RegisterSecretResponse, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving secrets management to separate file as runtime file is currently very big.
We can split it later as part of a refactor to jobs, resources and different apis.

cmd/secret.go Outdated
if updateSecretResponse.Success {
l.Info(coloredSuccess("Secret updated"))
} else {
return errors.New(fmt.Sprintf("Request failed for updating secret %s: %s", req.SecretName,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not start error with a capital letter as it could be embedded part of some other error. This is done in couple of places in this file

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the error messages to lowercase

cmd/secret.go Outdated
return errors.Wrapf(err, "Request failed for updating secret %s", req.SecretName)
}

if updateSecretResponse.Success {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what case this could be false?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is not required.

cmd/secret.go Outdated
secretRequestTimeout, secretRequestCancel := context.WithTimeout(context.Background(), secretTimeout)
defer secretRequestCancel()

l.Info("please wait...")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use a spinner here? and in Update call?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using spinner

return errors.Wrap(err, "unable to find secret by name")
}
resource, err := Secret{}.FromSpec(spec, repo.project, repo.hash)
return errors.New("secret already exist")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should generalize this error to resource already exists or something similar to store.ErrResourceNotFound or should get rid of this logic of checking if it exists and only insert the secret? Can you check if gorm throws an error if it already exists?
Because currently, I think we are leaking our implementation detail out of an interface.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gorm will throw an error like

\x1b[31;1m /../optimus/store/postgres/secret_repository.go:135 \x1b[35;1mERROR: duplicate key value violates unique constraint \"secret_project_id_name_key\" (SQLSTATE 23505)" ....(prints whole query with values) 

Which will return the implementation (and query) details back in the api, so we will need to replace the returned error.

Can you please explain how are we leaking implementation details out of interface ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checking explicitly is fine & we don't need to rely on gorm error.

@sbchaos
Copy link
Contributor

sbchaos commented Jan 25, 2022

Merging the PR, we can still have the conversation if needed.

Any required changes will be made to the followup PR.

@sbchaos sbchaos merged commit 500e3cd into main Jan 25, 2022
@sbchaos sbchaos deleted the secret-management branch January 25, 2022 08:08
@sravankorumilli sravankorumilli linked an issue Feb 16, 2022 that may be closed by this pull request
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

User should be able to create/update a secret through apis & cli.

7 participants