GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — KMS changes by daniel-sanche · Pull Request #1723 · GoogleCloudPlatform/python-docs-samples

Conversation

@daniel-sanche

@daniel-sanche daniel-sanche commented Sep 25, 2018

Copy link
Copy Markdown
Member

implements changes suggested in b/113025362 and b/113025156

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
daniel-sanche added 3 commits September 21, 2018 13:59
@daniel-sanche
added region tag for imports
ecc131c
@daniel-sanche
signing works with bytes
ca42575
@daniel-sanche
encrypt/decrypt uses bytes
c4efd87
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Sep 25, 2018
daniel-sanche added 4 commits September 25, 2018 14:55
@daniel-sanche
fixed style issues
91a4324
@daniel-sanche
moved location of base64 encoding
fc79ceb
@daniel-sanche
renamed message to plaintext
6e0d35e
@daniel-sanche
modified comments
09927c4
russ-
russ- suggested changes Sep 27, 2018
View reviewed changes
Comment thread kms/api-client/asymmetric.py Outdated
Decrypt a given ciphertext using an 'RSA_DECRYPT_OAEP_2048_SHA256' private
key stored on Cloud KMS
"""
request_body = {'ciphertext': ciphertext.decode()}

@russ- russ- Sep 27, 2018

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

In the encrypt snippet0, the corresponding request field is converted to base64 by the snippet. I believe that needs to happen here as well.

kms.projects().locations().keyRings().cryptoKeys().cryptoKeyVersions().asymmetricDecrypt(name='projects/google.com:rra-kms-quickstart/locations/global/keyRings/foo-ring/cryptoKeys/foo-key/cryptoKeyVersions/1', body={'ciphertext': 'foobar'}).execute()
...
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://cloudkms.googleapis.com/v1/projects/google.com:rra-kms-quickstart/locations/global/keyRings/foo-ring/cryptoKeys/foo-key/cryptoKeyVersions/1:asymmetricDecrypt?alt=json returned "Invalid value at 'ciphertext' (TYPE_BYTES),">

I believe the binary data inputs in the body of each request should always be like "base64.b64encode(data).decode('ascii')". This leads to the return type matching the input type (in this case, plaintext comes from base64.b64decode, which returns bytes). I think it'd be best to also document that the input must be "bytes", or the equivalent python 2 type (which I think is str).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread kms/api-client/asymmetric.py Outdated
ciphertext = base64.b64encode(ciphertext).decode('utf-8')
return ciphertext
ciphertext = public_key.encrypt(plaintext, pad)
return base64.b64encode(ciphertext)

@russ- russ- Sep 27, 2018

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This should return the bytes returned from public_key.encrypt, without base64 encoding it.

In general, treat base64 as a part of the JSON encoding, because the only reason base64 is used is because JSON only supports UTF-8 strings. base64 is a shim to make non-UTF-8 data compatible with JSON. For that reason, it's clearest to keep the base64 encoding/decoding right before/after the actual HTTP call.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@daniel-sanche daniel-sanche Sep 27, 2018

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

make sense, I moved the encoding into the decrypt request body

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread kms/api-client/asymmetric_test.py Outdated
# ciphertext should be 344 characters with base64 and RSA 2048
assert len(ciphertext) == 344, \
'ciphertext should be 344 chars; got {}'.format(len(ciphertext))
assert ciphertext[-2:] == '==', 'cipher text should end with =='

@russ- russ- Sep 27, 2018

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This is an odd detail to assert, and I'm surprised the test isn't flaky because of it. The API doesn't guarantee anything about the content of the ciphertext. I'd remove this assertion.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@daniel-sanche daniel-sanche Sep 27, 2018

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

removed

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread kms/api-client/asymmetric_test.py Outdated
self.client,
self.rsaSign)
assert success is True, 'RSA verification failed'
changed_bytes = (self.message+".").encode('utf-8')

@russ- russ- Sep 27, 2018

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Nit: alternatively, changed+bytes = message_bytes + b'.'.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@daniel-sanche daniel-sanche Sep 27, 2018

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

That does look nicer. Changed

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@daniel-sanche
daniel-sanche force-pushed the kms-changes branch from 6e472c5 to 800ae10 Compare September 27, 2018 19:54
@daniel-sanche
addressed PR comments
e85865b
@daniel-sanche
daniel-sanche force-pushed the kms-changes branch from 800ae10 to e85865b Compare September 27, 2018 19:56
russ-
russ- suggested changes Sep 28, 2018
View reviewed changes

@russ- russ- left a comment

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread kms/api-client/asymmetric.py Outdated

digest_bytes = hashlib.sha256(message.encode('ascii')).digest()
digest_bytes = hashlib.sha256(message).digest()
sig_bytes = base64.b64decode(signature)

@russ- russ- Sep 28, 2018

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The signature passed to verifySignature{RSA,EC} should be raw binary. signAsymmetric should b64decode the signature received in the response.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@daniel-sanche daniel-sanche Sep 28, 2018

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

fixed

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread kms/api-client/asymmetric_test.py
self.client,
self.rsaDecrypt)
assert plaintext_bytes == self.message_bytes
plaintext = plaintext_bytes.decode('utf-8')

@russ- russ- Sep 28, 2018

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Is there much value in checking that plaintext == self.message if you've already checked that plaintext_bytes == self.message_bytes?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@daniel-sanche daniel-sanche Sep 28, 2018

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Probably not, but I'd rather err on the side of too many tests, especially when dealing with all these encoding changes

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@engelke engelke Sep 28, 2018

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Most of the time you specify utf-8 for string.encode() and bytes.decode(), except on line 51 where you just assume the default (which is utf-8). Both ways work fine, but this is a minor inconsistency.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@daniel-sanche daniel-sanche Sep 28, 2018

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

good point, we should probably keep that consistent. Fixed

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@daniel-sanche
sign/verify uses bytes instead of String
927e3cc
@daniel-sanche
daniel-sanche force-pushed the kms-changes branch from f3b9b79 to 927e3cc Compare September 28, 2018 19:49
engelke
engelke approved these changes Sep 28, 2018
View reviewed changes

@engelke engelke left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@daniel-sanche
decode utf-8
293e605
@daniel-sanche
daniel-sanche merged commit 172ee1d into master Sep 28, 2018
@jmdobry
jmdobry deleted the kms-changes branch October 16, 2018 19:11
busunkim96 pushed a commit to googleapis/python-kms that referenced this pull request Jun 4, 2020
@daniel-sanche @busunkim96
KMS changes [(#1723)](GoogleCloudPlatform/python-docs-samples#1723)
fdcbe3f
use byte parameters instead of strings
@rsamborski rsamborski mentioned this pull request Nov 8, 2022
migrate code from googleapis/python-kms #8470
Merged
6 of 8 tasks
rsamborski pushed a commit that referenced this pull request Nov 8, 2022
@daniel-sanche @rsamborski
KMS changes [(#1723)](#1723)
666de00
use byte parameters instead of strings
rsamborski pushed a commit that referenced this pull request Nov 11, 2022
@daniel-sanche @rsamborski
KMS changes [(#1723)](#1723)
bcaf434
use byte parameters instead of strings
rsamborski pushed a commit that referenced this pull request Nov 14, 2022
@daniel-sanche @rsamborski
KMS changes [(#1723)](#1723)
909a24b
use byte parameters instead of strings
dandhlee pushed a commit that referenced this pull request Nov 14, 2022
@daniel-sanche @dandhlee
KMS changes [(#1723)](#1723)
188fba9
use byte parameters instead of strings
parthea pushed a commit to googleapis/google-cloud-python that referenced this pull request Oct 21, 2023
@daniel-sanche @busunkim96
KMS changes [(#1723)](GoogleCloudPlatform/python-docs-samples#1723)
7d9aa27
use byte parameters instead of strings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

2 more reviewers
@russ- russ- russ- requested changes
@engelke engelke engelke approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@daniel-sanche @engelke @russ- @googlebot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.