-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Domain Ownership Service implemented with AppRegistry * Devcontainer includes mock App Registry
- Loading branch information
Showing
29 changed files
with
325 additions
and
153 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"domain-names": [ | ||
{ | ||
"id": "example.com", | ||
"fullyQualifiedDomainName": "example.com", | ||
"ownerDelegatedRequestsToTeam": true, | ||
"autoApprovedGroups": "group1", | ||
"autoApprovedServiceAccounts": "[email protected]" | ||
}, | ||
{ | ||
"id": "example2.com", | ||
"fullyQualifiedDomainName": "example2.com", | ||
"ownerDelegatedRequestsToTeam": true, | ||
"autoApprovedGroups": "group1", | ||
"autoApprovedServiceAccounts": "[email protected]" | ||
} | ||
] | ||
} |
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,3 @@ | ||
{ | ||
"/api/v1beta1/*": "/$1" | ||
} |
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 |
---|---|---|
|
@@ -19,6 +19,8 @@ services: | |
VAULT_ADDR: http://10.1.10.100:8200 | ||
VAULT_TOKEN: root_token | ||
JWT_SIGNING_KEY: jwt_secret | ||
APP_REGISTRY_ADDR: http://10.1.10.150:8800 | ||
APP_REGISTRY_TOKEN: app_reg_token | ||
|
||
vault: | ||
image: hashicorp/vault:latest | ||
|
@@ -32,6 +34,20 @@ services: | |
astral: | ||
ipv4_address: "10.1.10.100" | ||
|
||
app_registry: | ||
image: node:latest | ||
restart: unless-stopped | ||
ports: | ||
- 8800:8800 | ||
volumes: | ||
- .:/data | ||
networks: | ||
astral: | ||
ipv4_address: "10.1.10.150" | ||
command: > | ||
sh -c "npm install -g [email protected] && | ||
json-server /data/app_reg_db.json --routes /data/app_reg_routes.json --port 8800 --host 0.0.0.0" | ||
networks: | ||
astral: | ||
ipam: | ||
|
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
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
class IssueCert | ||
include Interactor::Organizer | ||
include FailOnError | ||
|
||
organize AuthorizeRequest, ObtainCert, Log | ||
organize RefreshDomain, AuthorizeRequest, ObtainCert, Log | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class RefreshDomain | ||
include Interactor | ||
|
||
def call | ||
domain_info = Services::DomainOwnershipService.get_domain_info(context.request.common_name) | ||
domain_record = Domain.find_or_create_by!(fqdn: context.request.common_name) | ||
if !domain_info | ||
domain_record.destroy! | ||
return | ||
end | ||
|
||
domain_record.update!( | ||
group_delegation: domain_info.group_delegation, | ||
groups: domain_info.groups, | ||
users: domain_info.users | ||
) | ||
rescue => e | ||
Rails.logger.warn("Continuing after error in #{self.class.name}: #{e.class.name}: #{e.message}") | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
module Services | ||
class AppRegistryService | ||
class << self | ||
def get_domain_info(fqdn) | ||
rslt = client.get("/api/v1beta1/domain-names/#{fqdn}").body | ||
convert(rslt) | ||
rescue Faraday::ResourceNotFound => e | ||
nil | ||
end | ||
|
||
private | ||
|
||
def client | ||
Faraday.new(ssl: ssl_opts, url: Rails.configuration.astral[:app_registry_addr]) do |faraday| | ||
faraday.request :authorization, "Bearer", -> { Rails.configuration.astral[:app_registry_token] } | ||
faraday.request :retry, retry_opts | ||
faraday.response :json | ||
faraday.response :raise_error, include_request: true | ||
end | ||
end | ||
|
||
def convert(domain_info) | ||
if !domain_info || domain_info["isDeleted"] | ||
return nil | ||
end | ||
|
||
OpenStruct.new( | ||
fqdn: domain_info["fullyQualifiedDomainName"], | ||
group_delegation: domain_info["ownerDelegatedRequestsToTeam"], | ||
groups: domain_info["autoApprovedGroups"], | ||
users: domain_info["autoApprovedServiceAccounts"] | ||
) | ||
end | ||
|
||
def ssl_opts | ||
{ | ||
ca_file: Rails.configuration.astral[:app_registry_ca_file], | ||
client_cert: Rails.configuration.astral[:app_registry_client_cert], | ||
client_key: Rails.configuration.astral[:app_registry_client_key] | ||
} | ||
end | ||
|
||
def retry_opts | ||
{ | ||
max: 3, | ||
interval: 0.05, | ||
interval_randomness: 0.5, | ||
backoff_factor: 2 | ||
} | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,23 @@ | ||
module Services | ||
class AuthService | ||
def initialize | ||
@domain_ownership_service = DomainOwnershipService.new | ||
end | ||
|
||
def authenticate!(token) | ||
identity = decode(token) | ||
raise AuthError unless identity | ||
# TODO verify identity with authority? | ||
identity | ||
end | ||
|
||
def authorize!(identity, cert_issue_req) | ||
@domain_ownership_service.authorize!(identity, cert_issue_req) | ||
end | ||
class << self | ||
def authenticate!(token) | ||
identity = decode(token) | ||
raise AuthError unless identity | ||
# TODO verify identity with authority? | ||
identity | ||
end | ||
|
||
private | ||
private | ||
|
||
def decode(token) | ||
# Decode a JWT access token using the configured base. | ||
body = JWT.decode(token, Rails.application.config.astral[:jwt_signing_key])[0] | ||
Identity.new(body) | ||
rescue => e | ||
Rails.logger.warn "Unable to decode token: #{e}" | ||
nil | ||
def decode(token) | ||
# Decode a JWT access token using the configured base. | ||
body = JWT.decode(token, Rails.configuration.astral[:jwt_signing_key])[0] | ||
Identity.new(body) | ||
rescue => e | ||
Rails.logger.warn "Unable to decode token: #{e}" | ||
nil | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
module Services | ||
class CertificateService | ||
def initialize | ||
# TODO this should select an implementation service based on config | ||
@impl = VaultService.new | ||
end | ||
class << self | ||
def issue_cert(cert_issue_request) | ||
impl.issue_cert(cert_issue_request) | ||
end | ||
|
||
private | ||
|
||
def issue_cert(cert_issue_request) | ||
@impl.issue_cert(cert_issue_request) | ||
def impl | ||
# TODO this should select an implementation service based on config | ||
VaultService | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
module Services | ||
class DomainOwnershipService | ||
def authorize!(identity, cert_req) | ||
cert_req.fqdns.each do |fqdn| | ||
domain = Domain.where(fqdn: fqdn).first | ||
raise AuthError unless domain.present? && | ||
(domain.owner == identity.subject || | ||
(domain.group_delegation && | ||
(domain.groups & identity.groups).any?)) | ||
class << self | ||
def get_domain_info(fqdn) | ||
impl.get_domain_info(fqdn) | ||
end | ||
|
||
private | ||
|
||
def impl | ||
# TODO this should select an implementation service based on config | ||
AppRegistryService | ||
end | ||
nil | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,22 @@ | ||
module Services | ||
class VaultService | ||
def initialize | ||
# TODO create a new token for use in the session | ||
@client = Vault::Client.new( | ||
address: Rails.application.config.astral[:vault_addr], | ||
token: Rails.application.config.astral[:vault_token] | ||
) | ||
end | ||
class << self | ||
def issue_cert(cert_issue_request) | ||
opts = cert_issue_request.attributes | ||
# Generate the TLS certificate using the intermediate CA | ||
tls_cert = client.logical.write(Rails.configuration.astral[:vault_cert_path], opts) | ||
OpenStruct.new tls_cert.data | ||
end | ||
|
||
private | ||
|
||
def issue_cert(cert_issue_request) | ||
opts = cert_issue_request.attributes | ||
# Generate the TLS certificate using the intermediate CA | ||
tls_cert = @client.logical.write(Rails.application.config.astral[:vault_cert_path], opts) | ||
OpenStruct.new tls_cert.data | ||
def client | ||
# TODO create a new token for use in the session | ||
Vault::Client.new( | ||
address: Rails.configuration.astral[:vault_addr], | ||
token: Rails.configuration.astral[:vault_token] | ||
) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
class Domain < ApplicationRecord | ||
serialize :groups, coder: YAML, type: Array | ||
before_save :clean_groups | ||
validates :fqdn, presence: true | ||
|
||
validates :fqdn, :owner, presence: true | ||
def groups_array | ||
(groups || "").split(",").sort.uniq | ||
end | ||
|
||
def clean_groups | ||
self.groups = groups.sort.uniq | ||
def users_array | ||
(users || "").split(",").sort.uniq | ||
end | ||
end |
Oops, something went wrong.