-
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.
Merge pull request #2 from G-Research/astral-poc
Astral poc
- Loading branch information
Showing
20 changed files
with
469 additions
and
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,30 +13,10 @@ jobs: | |
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Set up Ruby | ||
uses: ruby/setup-ruby@v1 | ||
with: | ||
ruby-version: .ruby-version | ||
bundler-cache: true | ||
|
||
- name: Scan for common Rails security vulnerabilities using static analysis | ||
run: bin/brakeman --no-pager | ||
|
||
scan_js: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Set up Ruby | ||
uses: ruby/setup-ruby@v1 | ||
- name: Run brakeman | ||
uses: devcontainers/[email protected] | ||
with: | ||
ruby-version: .ruby-version | ||
bundler-cache: true | ||
|
||
- name: Scan for security vulnerabilities in JavaScript dependencies | ||
run: bin/importmap audit | ||
runCmd: bin/brakeman --no-pager | ||
|
||
lint: | ||
runs-on: ubuntu-latest | ||
|
@@ -63,23 +43,13 @@ jobs: | |
# - 6379:6379 | ||
# options: --health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5 | ||
steps: | ||
- name: Install packages | ||
run: sudo apt-get update && sudo apt-get install --no-install-recommends -y google-chrome-stable curl libjemalloc2 libsqlite3-0 libvips | ||
|
||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Set up Ruby | ||
uses: ruby/setup-ruby@v1 | ||
with: | ||
ruby-version: .ruby-version | ||
bundler-cache: true | ||
|
||
- name: Run tests | ||
env: | ||
RAILS_ENV: test | ||
# REDIS_URL: redis://localhost:6379/0 | ||
run: bin/rails db:test:prepare test test:system | ||
uses: devcontainers/[email protected] | ||
with: | ||
runCmd: bin/rails test | ||
|
||
- name: Keep screenshots from failed system tests | ||
uses: actions/upload-artifact@v4 | ||
|
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,24 +1,25 @@ | ||
# README | ||
|
||
This README would normally document whatever steps are necessary to get the | ||
application up and running. | ||
Astral-rails is a proof-of-concept api application intended to simplify | ||
certificate acquisition for other applications/services. Broadly speaking, | ||
it will: | ||
|
||
Things you may want to cover: | ||
1) Authorize the request for cerficate using a third party trusted source (JWT, etc) | ||
2) If authorized, obtain a certificate from PKI CLM (such as Vault/OpenBao) | ||
3) Log this transaction in audit infrastructure (ELK, etc). | ||
|
||
* Ruby version | ||
# Running | ||
|
||
* System dependencies | ||
This app is most easily run and developed in its devcontainer. | ||
|
||
* Configuration | ||
1) Open in devcontainer | ||
2) Launch server using vscode launch config, or in terminal run: | ||
``` | ||
rails s | ||
``` | ||
3) POST /certificates to acquire cert in terminal: | ||
``` | ||
curl -X POST http://localhost:3000/certificates \ | ||
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJhcHBsaWNhdGlvbl9uYW1lIiwiY29tbW9uX25hbWUiOiJleGFtcGxlLmNvbSIsImlwX3NhbnMiOiIxMC4wLjEuMTAwIn0.61e0oQIj7vwGtOpFuPJDCI_Bqf8ZTpJxe_2kUwcbN7Y" | ||
``` | ||
|
||
* Database creation | ||
|
||
* Database initialization | ||
|
||
* How to run the test suite | ||
|
||
* Services (job queues, cache servers, search engines, etc.) | ||
|
||
* Deployment instructions | ||
|
||
* ... |
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,2 +1,35 @@ | ||
class ApplicationController < ActionController::API | ||
rescue_from StandardError, with: :handle_standard_error | ||
rescue_from AuthError, with: :handle_auth_error | ||
rescue_from ActionController::ParameterMissing, with: :handle_bad_request | ||
|
||
attr_reader :identity # decoded and verified JWT | ||
|
||
def info | ||
render json: { | ||
app: "astral", | ||
description: "Astral provides a simplified API for PKI.", | ||
version: "0.0.1" | ||
} | ||
end | ||
|
||
def authenticate_request | ||
token = request.headers["Authorization"] | ||
token = token.split(" ").last if token | ||
@identity = Services::AuthService.new.authenticate!(token) | ||
end | ||
|
||
private | ||
|
||
def handle_standard_error(exception) | ||
render json: { error: exception.message }, status: :internal_server_error | ||
end | ||
|
||
def handle_auth_error(exception) | ||
render json: { error: "Unauthorized" }, status: :unauthorized | ||
end | ||
|
||
def handle_bad_request(exception) | ||
render json: { error: exception }, status: :bad_request | ||
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,36 @@ | ||
class CertificatesController < ApplicationController | ||
before_action :authenticate_request | ||
|
||
def create | ||
req = CertIssueRequest.new(params_permitted) | ||
if !req.valid? | ||
render json: { error: req.errors }, status: :bad_request | ||
else | ||
cert = Services::CertificateService.new.issue_cert(req) | ||
render json: cert | ||
end | ||
end | ||
|
||
private | ||
|
||
def params_permitted | ||
attrs = %i[ common_name | ||
alt_names | ||
exclude_cn_from_sans | ||
format | ||
not_after | ||
other_sans | ||
private_key_format | ||
remove_roots_from_chain | ||
ttl | ||
uri_sans | ||
ip_sans | ||
serial_number | ||
client_flag | ||
code_signing_flag | ||
email_protection_flag | ||
server_flag | ||
] | ||
params.permit(attrs) | ||
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,3 @@ | ||
# Error representing a failed authentication | ||
class AuthError < StandardError | ||
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,32 @@ | ||
module Services | ||
class AppRegistryService | ||
def authenticate!(token) | ||
identity = decode(token) | ||
raise AuthError unless identity | ||
# TODO verify identity with authority? | ||
identity | ||
end | ||
|
||
def authorize!(identity, cert_req) | ||
cert_req.fqdns.each do |fqdn| | ||
domain = get_domain_name(fqdn) | ||
raise AuthError unless (domain[:auto_approved_groups] & identity[:groups]).any? | ||
end | ||
end | ||
|
||
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] | ||
HashWithIndifferentAccess.new body | ||
rescue => e | ||
Rails.logger.warn "Unable to decode token: #{e}" | ||
nil | ||
end | ||
|
||
def get_domain_name(fqdn) | ||
# TODO implement | ||
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Services | ||
class AuthService | ||
def initialize | ||
# TODO make this selectable | ||
@impl = AppRegistryService.new | ||
end | ||
|
||
def authenticate!(token) | ||
@impl.authenticate!(token) | ||
end | ||
|
||
def authorize!(token, cert_issue_req) | ||
@impl.authorize!(token, cert_issue_req) | ||
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Services | ||
class CertificateService | ||
def initialize | ||
# TODO this should select an implementation service based on config | ||
@impl = VaultService.new | ||
end | ||
|
||
def issue_cert(cert_issue_request) | ||
@impl.issue_cert(cert_issue_request) | ||
end | ||
end | ||
end |
Oops, something went wrong.