Skip to content

Commit cd96f0d

Browse files
authored
Generate passwords and TLS certificates at provisioning time (#359)
1 parent e1c5099 commit cd96f0d

File tree

4 files changed

+65
-38
lines changed

4 files changed

+65
-38
lines changed

marketplace/deployer_util/expand_config.py

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@
1717
import base64
1818
import json
1919
import os
20-
import OpenSSL
21-
import random
2220
from argparse import ArgumentParser
2321

2422
import yaml
2523

2624
import config_helper
25+
import property_generator
2726
import schema_values_common
28-
from password import GeneratePassword
2927

3028
_PROG_HELP = """
3129
Modifies the configuration parameter files in a directory
@@ -98,12 +96,11 @@ def expand(values_dict, schema, app_uid=''):
9896
# thus is eligible for auto-generation.
9997
if v is None:
10098
if prop.password:
101-
v = generate_password(prop.password)
99+
v = property_generator.generate_password(prop.password)
102100
elif prop.application_uid:
103101
v = app_uid or ''
104-
generate_properties_for_appuid(prop, app_uid, generated)
105102
elif prop.tls_certificate:
106-
v = generate_tls_certificate()
103+
v = property_generator.generate_tls_certificate()
107104
elif prop.xtype == config_helper.XTYPE_ISTIO_ENABLED:
108105
# For backward compatibility.
109106
v = False
@@ -132,6 +129,8 @@ def expand(values_dict, schema, app_uid=''):
132129
raise InvalidProperty(
133130
'Invalid value for TLS_CERTIFICATE property {}: {}'.format(k, v))
134131
generate_properties_for_tls_certificate(prop, v, generated)
132+
elif prop.application_uid:
133+
generate_properties_for_appuid(prop, v, generated)
135134

136135
if v is not None:
137136
result[k] = v
@@ -247,37 +246,6 @@ def generate_properties_for_string(prop, value, result):
247246
result[prop.string.base64_encoded] = base64.b64encode(value)
248247

249248

250-
def generate_password(config):
251-
pw = GeneratePassword(config.length, config.include_symbols)
252-
if config.base64:
253-
pw = base64.b64encode(pw)
254-
return pw
255-
256-
257-
def generate_tls_certificate():
258-
cert_seconds_to_expiry = 60 * 60 * 24 * 365 # one year
259-
260-
key = OpenSSL.crypto.PKey()
261-
key.generate_key(OpenSSL.crypto.TYPE_RSA, 2048)
262-
263-
cert = OpenSSL.crypto.X509()
264-
cert.get_subject().OU = 'GCP Marketplace K8s App Tools'
265-
cert.get_subject().CN = 'Temporary Certificate'
266-
cert.gmtime_adj_notBefore(0)
267-
cert.gmtime_adj_notAfter(cert_seconds_to_expiry)
268-
cert.set_serial_number(random.getrandbits(64))
269-
cert.set_issuer(cert.get_subject())
270-
cert.set_pubkey(key)
271-
cert.sign(key, 'sha256')
272-
273-
return json.dumps({
274-
'private_key':
275-
OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, key),
276-
'certificate':
277-
OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
278-
})
279-
280-
281249
def generate_properties_for_tls_certificate(prop, value, result):
282250
certificate = json.loads(value)
283251
if prop.tls_certificate.base64_encoded_private_key:
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import base64
16+
import json
17+
import OpenSSL
18+
import random
19+
20+
from password import GeneratePassword
21+
22+
23+
def generate_password(config):
24+
"""Generate password value for SchemaXPassword config."""
25+
pw = GeneratePassword(config.length, config.include_symbols)
26+
if config.base64:
27+
pw = base64.b64encode(pw)
28+
return pw
29+
30+
31+
def generate_tls_certificate():
32+
"""Generate TLS value, a json string."""
33+
cert_seconds_to_expiry = 60 * 60 * 24 * 365 # one year
34+
35+
key = OpenSSL.crypto.PKey()
36+
key.generate_key(OpenSSL.crypto.TYPE_RSA, 2048)
37+
38+
cert = OpenSSL.crypto.X509()
39+
cert.get_subject().OU = 'GCP Marketplace K8s App Tools'
40+
cert.get_subject().CN = 'Temporary Certificate'
41+
cert.gmtime_adj_notBefore(0)
42+
cert.gmtime_adj_notAfter(cert_seconds_to_expiry)
43+
cert.set_serial_number(random.getrandbits(64))
44+
cert.set_issuer(cert.get_subject())
45+
cert.set_pubkey(key)
46+
cert.sign(key, 'sha256')
47+
48+
return json.dumps({
49+
'private_key':
50+
OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, key),
51+
'certificate':
52+
OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
53+
})

marketplace/deployer_util/provision.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import yaml
2222

2323
import config_helper
24+
import property_generator
2425
import schema_values_common
2526
import storage
2627

@@ -97,8 +98,12 @@ def process(schema, values, deployer_image, deployer_entrypoint, version_repo,
9798
# TODO: Really populate this value.
9899
props[prop.name] = False
99100
elif prop.xtype == config_helper.XTYPE_INGRESS_AVAILABLE:
100-
# TODO: Really populate this value.
101+
# TODO(#360): Really populate this value.
101102
props[prop.name] = True
103+
elif prop.password:
104+
props[prop.name] = property_generator.generate_password(prop.password)
105+
elif prop.tls_certificate:
106+
props[prop.name] = property_generator.generate_tls_certificate()
102107

103108
# Merge input and provisioned properties.
104109
app_params = dict(list(values.iteritems()) + list(props.iteritems()))

marketplace/dev/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
99
jq \
1010
make \
1111
python \
12+
python-openssl \
1213
python-pip \
1314
python-setuptools \
1415
python-yaml \

0 commit comments

Comments
 (0)