-
Notifications
You must be signed in to change notification settings - Fork 657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add release property validation #2602
Open
jpalermo
wants to merge
2
commits into
main
Choose a base branch
from
pr-add-property-schema-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
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.
Oops, something went wrong.
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.
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.
It would be great to start a trend of moving magic strings like
'properties_schema.json'
into constants.