-
Notifications
You must be signed in to change notification settings - Fork 657
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Releases can add a properties_schema.json file at the root of their jobs that is a json schema file. This schema will be used to validate deployment properties prior to template rendering during a deploy.
- Loading branch information
Showing
19 changed files
with
404 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/bosh-director/lib/bosh/director/core/templates/job_schema_validator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'json_schemer' | ||
|
||
module Bosh::Director::Core::Templates | ||
|
||
class CustomType < JSONSchemer::Draft202012::Vocab::Validation::Type | ||
def error(formatted_instance_location:, **) | ||
case value | ||
when 'certificate' | ||
"value at #{formatted_instance_location} is not a certificate" | ||
else | ||
super | ||
end | ||
end | ||
|
||
def valid_type(type, instance) | ||
case type | ||
when 'certificate' | ||
return false unless instance.is_a?(String) | ||
return true if instance == "" | ||
begin | ||
OpenSSL::X509::Certificate.load(instance) | ||
rescue OpenSSL::X509::CertificateError => e | ||
false | ||
end | ||
else | ||
super | ||
end | ||
end | ||
end | ||
|
||
JSONSchemer::Draft202012::Vocab::VALIDATION['type'] = CustomType | ||
|
||
class JobSchemaValidator | ||
def self.validate(job_name:, schema:, properties:) | ||
raise "You must declare your $schema draft version" if schema['$schema'].blank? | ||
json_schemer_schema = JSONSchemer.schema(schema) | ||
raise "Only https://json-schema.org/draft/2020-12/schema schema is currently supported" unless json_schemer_schema.meta_schema.base_uri == JSONSchemer::Draft202012::BASE_URI | ||
errors = json_schemer_schema.validate(properties).map do |error| | ||
error['error'] | ||
end | ||
return true if errors.empty? | ||
errors.unshift("Error validating properties for #{job_name}") | ||
raise errors.join("\n") | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module CertificateHelpers | ||
class KeyCache | ||
@key = nil | ||
class << self | ||
attr_accessor :key | ||
end | ||
end | ||
|
||
def generate_rsa_certificate(sans: []) | ||
rsa_private_key = KeyCache.key | ||
if !rsa_private_key | ||
rsa_private_key = OpenSSL::PKey::RSA.generate(512) #Smallest key OpenSSL will currently allow to not waste time on cpu cycles | ||
KeyCache.key = rsa_private_key | ||
end | ||
rsa_cert = generate_cert(rsa_private_key, sans: sans) | ||
private_key_cipher = OpenSSL::Cipher.new 'aes-256-cbc' | ||
{ | ||
:cert_pem => rsa_cert.to_pem, | ||
:public_key_pem => rsa_private_key.public_to_pem, | ||
:private_key_pem => rsa_private_key.to_pem, | ||
} | ||
end | ||
|
||
def generate_cert(key, sans: []) | ||
cert = OpenSSL::X509::Certificate.new | ||
cert.version = 2 # cf. RFC 5280 - to make it a "v3" certificate | ||
cert.serial = 1 # Not secure, but this is a test certificate | ||
cert.subject = OpenSSL::X509::Name.parse "/CN=Test CA" | ||
cert.issuer = cert.subject # root CA's are "self-signed" | ||
cert.public_key = key | ||
cert.not_before = 5.minutes.ago | ||
cert.not_after = cert.not_before + (2 * 7 * 24 * 60 * 60) # 2 weeks validity | ||
ef = OpenSSL::X509::ExtensionFactory.new | ||
ef.subject_certificate = cert | ||
ef.issuer_certificate = cert | ||
cert.add_extension(ef.create_extension("basicConstraints","CA:TRUE",true)) | ||
cert.add_extension(ef.create_extension("keyUsage","keyCertSign, cRLSign", true)) | ||
cert.add_extension(ef.create_extension("subjectKeyIdentifier","hash",false)) | ||
cert.add_extension(ef.create_extension("authorityKeyIdentifier","keyid:always",false)) | ||
cert.add_extension(ef.create_extension('subjectAltName', sans.join(','))) unless sans.empty? | ||
cert.sign(key, OpenSSL::Digest.new('SHA256')) | ||
cert | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.