generate the secret property automatically

This commit is contained in:
Jonathan Bennett 2019-11-27 11:20:43 -05:00
parent eda98d166d
commit d3dd7498e6
1 changed files with 20 additions and 7 deletions

View File

@ -1,27 +1,39 @@
// Secrets.swift.gyb
// Generated by Secrets.swift.gyb
%{
import os
secrets = ['FEED_WRANGLER_KEY']
def chunks(seq, size):
return (seq[i:(i + size)] for i in range(0, len(seq), size))
return (seq[i:(i + size)] for i in range(0, len(seq), size))
def encode(string, salt):
bytes = string.encode("UTF-8")
return [ord(bytes[i]) ^ salt[i % len(salt)] for i in range(0, len(bytes))]
bytes = string.encode("UTF-8")
return [ord(bytes[i]) ^ salt[i % len(salt)] for i in range(0, len(bytes))]
def snake_to_camel(snake_str):
components = snake_str.split('_')
return components[0].lower() + ''.join(x.title() for x in components[1:])
salt = [ord(byte) for byte in os.urandom(64)]
}%
enum Secrets {
static var FeedWranglerClientKey: String {
let encoded: [UInt8] = [
% for chunk in chunks(encode(os.environ.get('FEED_WRANGLER_KEY'), salt), 8):
% for secret in secrets:
static var ${snake_to_camel(secret)}: String {
let encoded: [UInt8] = [
% for chunk in chunks(encode(os.environ.get(secret), salt), 8):
${"".join(["0x%02x, " % byte for byte in chunk])}
% end
]
return decode(encoded, salt: salt)
}
% end
}
private extension Secrets {
private static let salt: [UInt8] = [
% for chunk in chunks(salt, 8):
@ -34,4 +46,5 @@ enum Secrets {
element ^ salt[offset % salt.count]
}, as: UTF8.self)
}
}