Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command to protect text using crypto implemented #104

Merged
merged 8 commits into from
Mar 15, 2023

Conversation

jan-kowalczyk-wttech
Copy link
Contributor

No description provided.

@jan-kowalczyk-wttech jan-kowalczyk-wttech linked an issue Mar 14, 2023 that may be closed by this pull request
pkg/crypto.go Outdated
defer func(bodyReader io.ReadCloser) {
err := bodyReader.Close()
if err != nil {
log.Errorf("%s > cloud not close reader: %s", c.instance.ID(), err)
Copy link
Contributor

Choose a reason for hiding this comment

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

please recall my thoughts regarding context sensitive logging;

imagine me being a qa and seeing in console error z"local_author > cloud not close reader"` - this gives me nothing informative

Copy link
Contributor

Choose a reason for hiding this comment

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

hmm and IMO you overcomplicated reading response; please check other places in which I am unmarshalling response, for example: https://github.com/wttech/aemc/blob/main/pkg/osgi_event_manager.go#L26

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, this UnmarshalJSON error checker looks cleaner, but I have question, why are you not closing your reader? I think, It can generate leaks.

Copy link
Contributor

Choose a reason for hiding this comment

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

hmmm you are right, I need to check few other places then as well

Copy link
Contributor

Choose a reason for hiding this comment

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

but checking closing IMO is redundant / is a tradeoff as 3rd party (web server) will not have a chance to unintentionally fail well working program on our side

Copy link
Contributor

Choose a reason for hiding this comment

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

hmmm I want to avoid repetitions of low-level code (boilerplate) and hide it in utility methods like fmtx.UnmarshalXX so we should rather put closing inside such methods instead of doing what you are doing

pkg/crypto.go Outdated
err = fmtx.UnmarshalJSON(bodyReader, &body)

if err != nil {
log.Errorf("%s > error during unmarshaling crypto response body: %s", c.instance.ID(), err)
Copy link
Contributor

Choose a reason for hiding this comment

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

please align the style of the error message to used in other functionalities

Copy link
Contributor

@krystian-panek-vmltech krystian-panek-vmltech left a comment

Choose a reason for hiding this comment

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

please address comments

@@ -81,11 +81,16 @@ func (c *CLI) cryptoProtectCmd() *cobra.Command {
c.Error(err)
return
}
c.SetOutput("value", protectedValue)
c.SetOutput("protected", protectedValue)
Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer naming style topic_trait which stands for not protectedValue but valueProtected because then vars are grouped logically / the same topic has the same string prefix;

how about valueProtected here; or just value as it was designed?

Copy link
Contributor Author

@jan-kowalczyk-wttech jan-kowalczyk-wttech Mar 14, 2023

Choose a reason for hiding this comment

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

For me valueProtected or value is also fine, just I thought to use same convention as in crypto response, just to make it consistent

Copy link
Contributor

Choose a reason for hiding this comment

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

you should follow own conventions instead of being influenced by convention coming from outside to make your tool users less confused

pkg/crypto.go Outdated
)

const (
EncryptJsonPath = "/system/console/crypto/.json"
Copy link
Contributor

Choose a reason for hiding this comment

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

Do not introduce additional names for same thing. Why not CryptoProtectPath

pkg/crypto.go Outdated
response, err := requestFormData.Post(EncryptJsonPath)

if err != nil {
return "", fmt.Errorf("%s > Error while encrypting text using crypto: %s", c.instance.ID(), err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do not capitalize text. Do not add dot in the end

Copy link
Contributor

Choose a reason for hiding this comment

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

Why this starts with "error while..." but other msgs with "cannot". Align the style used

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I missed this error :)

pkg/crypto.go Outdated
response, err := requestFormData.Post(EncryptJsonPath)

if err != nil {
return "", fmt.Errorf("%s > Error while encrypting text using crypto: %s", c.instance.ID(), err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this starts with "error while..." but other msgs with "cannot". Align the style used

pkg/crypto.go Outdated
defer func(bodyReader io.ReadCloser) {
err := bodyReader.Close()
if err != nil {
log.Errorf("%s > cannot close crypto response body reader: %s", c.instance.ID(), err)
Copy link
Contributor

Choose a reason for hiding this comment

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

From the end user perspective response body reader is a technical detail that you should not mention. But mention what is happening understandable for that end user who is not seeing a code while running the tool

pkg/crypto.go Outdated
var body CryptoResponseBody

if err = fmtx.UnmarshalJSON(bodyReader, &body); err != nil {
return "", fmt.Errorf("%s > cannot unmarshal crypto response body: %s", c.instance.ID(), err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Unmarshal is too techy word again. "Parse response" I am using in other places. Please align the style

@@ -86,6 +86,11 @@ func (c *CLI) cryptoProtectCmd() *cobra.Command {
},
}
cmd.Flags().StringP("value", "v", "", "Value to protect")
cmd.MarkFlagRequired("value")
err := cmd.MarkFlagRequired("value")
Copy link
Contributor

Choose a reason for hiding this comment

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

nope; just use _ / see other code occurrences

)

const (
CryptoProtectPath = "/system/console/crypto/.json"
Copy link
Contributor

Choose a reason for hiding this comment

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

fine :)

pkg/crypto.go Outdated
@@ -13,6 +19,10 @@ type Crypto struct {
keyBundleSymbolicName string
}

type CryptoResponseBody struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

it's more like CryptoProtectResult

pkg/crypto.go Outdated
return value, nil // TODO implement this
var form = map[string]any{"datum": value}
log.Infof("%s > Protecting text using crypto.", c.instance.ID())
requestFormData := c.instance.http.RequestFormData(form)
Copy link
Contributor

Choose a reason for hiding this comment

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

redundant variable; keep the style of other features having snippet like yours

pkg/crypto.go Outdated

bodyReader := response.RawBody()

defer func(bodyReader io.ReadCloser) {
Copy link
Contributor

Choose a reason for hiding this comment

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

this defer is not needed here.

ReadCloser means that when reader/unmarshaller ends reading then the object implementing this interface will also automatically close the source so you don't need to take care;

just pass response.RawBody() directly to the UnmarshalJSON

@jan-kowalczyk-wttech jan-kowalczyk-wttech force-pushed the 93-implement-crypto-protect-command branch from 4135dc6 to 3608087 Compare March 15, 2023 08:38
@@ -70,5 +79,20 @@ func (c Crypto) Setup(hmacFile string, masterFile string) (bool, error) {
}

func (c Crypto) Protect(value string) (string, error) {
return value, nil // TODO implement this
log.Infof("%s > Protecting text using crypto.", c.instance.ID())
Copy link
Contributor

Choose a reason for hiding this comment

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

do not capitalize words; it's a general go style

pkg/crypto.go Outdated
@@ -70,5 +79,20 @@ func (c Crypto) Setup(hmacFile string, masterFile string) (bool, error) {
}

func (c Crypto) Protect(value string) (string, error) {
return value, nil // TODO implement this
log.Infof("%s > Protecting text using crypto.", c.instance.ID())
requestFormData := c.instance.http.RequestFormData(map[string]any{"datum": value})
Copy link
Contributor

Choose a reason for hiding this comment

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

inline 83 & 84 line (remove requestFormData var

Copy link
Contributor

Choose a reason for hiding this comment

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

extra vars are fine when they are reused or are introducing better readability; I don't see any of these here

pkg/crypto.go Outdated
response, err := requestFormData.Post(CryptoProtectPath)

if err != nil {
return "", fmt.Errorf("%s > cannot encrypt text using crypto: %s", c.instance.ID(), err)
Copy link
Contributor

Choose a reason for hiding this comment

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

special : %w here when you are passing err to Errorf / for supporting stack traces, etc

@jan-kowalczyk-wttech jan-kowalczyk-wttech merged commit dd76b10 into main Mar 15, 2023
@jan-kowalczyk-wttech jan-kowalczyk-wttech deleted the 93-implement-crypto-protect-command branch March 15, 2023 09:53
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.

Implement crypto protect command
2 participants