Skip to content

Commit d756386

Browse files
author
Jon Wayne Parrott
committed
Adding compute engine private key generation sample.
Change-Id: I06ba22294f8f0d734b048b3e903d2f98178b4a5c
1 parent d5fc45e commit d756386

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2016 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Example of authenticating using access tokens directly on Compute Engine.
18+
19+
For more information, see the README.md under /compute.
20+
"""
21+
22+
# [START all]
23+
24+
import argparse
25+
import base64
26+
import os
27+
28+
from cryptography import x509
29+
from cryptography.hazmat.backends import default_backend
30+
from cryptography.hazmat.primitives import hashes
31+
from cryptography.hazmat.primitives.asymmetric import padding
32+
import requests
33+
34+
35+
GOOGLE_PUBLIC_CERT_URL = (
36+
'https://cloud-certs.storage.googleapis.com/google-cloud-csek-ingress.pem')
37+
38+
39+
def get_google_public_cert_key():
40+
r = requests.get(GOOGLE_PUBLIC_CERT_URL)
41+
r.raise_for_status()
42+
43+
# Load the certificate.
44+
certificate = x509.load_pem_x509_certificate(
45+
r.text.encode('utf-8'), default_backend())
46+
47+
# Get the certicate's public key.
48+
public_key = certificate.public_key()
49+
50+
return public_key
51+
52+
53+
def wrap_rsa_key(public_key, private_key_bytes):
54+
# Use the Google public key to encrypt the customer private key.
55+
# This means that only the Google private key is capable of decrypting
56+
# the customer private key.
57+
wrapped_key = public_key.encrypt(
58+
private_key_bytes,
59+
padding.OAEP(
60+
mgf=padding.MGF1(algorithm=hashes.SHA1()),
61+
algorithm=hashes.SHA1(),
62+
label=None))
63+
encoded_wrapped_key = base64.b64encode(wrapped_key)
64+
return encoded_wrapped_key
65+
66+
67+
def main(key_file):
68+
# Generate a new 256-bit private key if no key is specified.
69+
if not key_file:
70+
customer_key_bytes = os.urandom(32)
71+
else:
72+
with open(key_file, 'rb') as f:
73+
customer_key_bytes = f.read()
74+
75+
google_public_key = get_google_public_cert_key()
76+
wrapped_rsa_key = wrap_rsa_key(google_public_key, customer_key_bytes)
77+
78+
print('Base-64 encoded private key: {}'.format(
79+
base64.b64encode(customer_key_bytes).decode('utf-8')))
80+
print('Wrapped RSA key: {}'.format(wrapped_rsa_key.decode('utf-8')))
81+
82+
83+
if __name__ == '__main__':
84+
parser = argparse.ArgumentParser(
85+
description=__doc__,
86+
formatter_class=argparse.RawDescriptionHelpFormatter)
87+
parser.add_argument(
88+
'--key_file', help='File containing your binary private key.')
89+
90+
args = parser.parse_args()
91+
92+
main(args.key_file)
93+
# [END all]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2016, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
import generate_wrapped_rsa_key
15+
16+
17+
def test_main():
18+
generate_wrapped_rsa_key.main()

compute/encryption/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cryptography==1.3.1
2+
requests==2.9.1

0 commit comments

Comments
 (0)