-
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 branch 'main' into interactors
- Loading branch information
Showing
13 changed files
with
209 additions
and
148 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 |
---|---|---|
@@ -1,66 +1,58 @@ | ||
# syntax = docker/dockerfile:1 | ||
|
||
# This Dockerfile is designed for production, not development. Use with Kamal or build'n'run by hand: | ||
# docker build -t my-app . | ||
# docker run -d -p 80:80 -p 443:443 --name my-app -e RAILS_MASTER_KEY=<value from config/master.key> my-app | ||
|
||
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version | ||
ARG RUBY_VERSION=3.3.4 | ||
FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base | ||
ARG RAILS_ROOT=/app | ||
FROM ruby:$RUBY_VERSION-alpine AS builder | ||
|
||
# Rails app lives here | ||
WORKDIR /rails | ||
|
||
# Install base packages | ||
RUN apt-get update -qq && \ | ||
apt-get install --no-install-recommends -y curl libjemalloc2 libsqlite3-0 libvips && \ | ||
rm -rf /var/lib/apt/lists /var/cache/apt/archives | ||
# Install build dependencies | ||
RUN apk add --no-cache build-base git pkgconfig | ||
|
||
# Set production environment | ||
ENV RAILS_ENV="production" \ | ||
RAILS_ROOT="/app" \ | ||
BUNDLE_DEPLOYMENT="1" \ | ||
BUNDLE_PATH="/usr/local/bundle" \ | ||
BUNDLE_WITHOUT="development" | ||
|
||
# Throw-away build stage to reduce size of final image | ||
FROM base AS build | ||
BUNDLE_PATH="/app/.bundle" \ | ||
BUNDLE_WITHOUT="test development" | ||
|
||
# Install packages needed to build gems | ||
RUN apt-get update -qq && \ | ||
apt-get install --no-install-recommends -y build-essential git pkg-config && \ | ||
rm -rf /var/lib/apt/lists /var/cache/apt/archives | ||
WORKDIR $RAILS_ROOT | ||
|
||
# Install application gems | ||
# Install gems | ||
COPY Gemfile Gemfile.lock ./ | ||
RUN bundle install && \ | ||
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \ | ||
bundle exec bootsnap precompile --gemfile | ||
RUN bundle config --global frozen 1 \ | ||
&& bundle config set path 'vendor/bundle' \ | ||
&& bundle install --without development:test -j4 --retry 3 \ | ||
&& rm -rf vendor/bundle/ruby/3.3.0/cache/*.gem # \ | ||
&& find vendor/bundle/ruby/3.3.0/gems/ -name "*.c" -delete \ | ||
&& find vendor/bundle/ruby/3.3.0/gems/ -name "*.o" -delete | ||
|
||
# Copy application code | ||
COPY . . | ||
|
||
# Precompile bootsnap code for faster boot times | ||
# Precompile bootsnap | ||
RUN bundle exec bootsnap precompile app/ lib/ | ||
|
||
# Final stage | ||
FROM ruby:$RUBY_VERSION-alpine | ||
|
||
# Install runtime dependencies | ||
RUN apk add --no-cache curl jemalloc sqlite-libs vips tzdata | ||
|
||
ENV RAILS_ENV="production" \ | ||
RAILS_ROOT="/app" \ | ||
BUNDLE_DEPLOYMENT="1" \ | ||
BUNDLE_APP_CONFIG="/app/.bundle" \ | ||
BUNDLE_WITHOUT="test development" | ||
|
||
# Final stage for app image | ||
FROM base | ||
WORKDIR $RAILS_ROOT | ||
|
||
# Copy built artifacts: gems, application | ||
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}" | ||
COPY --from=build /rails /rails | ||
# Copy built artifacts | ||
COPY --from=builder $RAILS_ROOT $RAILS_ROOT | ||
|
||
# Run and own only the runtime files as a non-root user for security | ||
RUN groupadd --system --gid 1000 rails && \ | ||
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \ | ||
# Add non-root user | ||
RUN addgroup -S rails && adduser -S rails -G rails && \ | ||
chown -R rails:rails db log storage tmp | ||
USER 1000:1000 | ||
|
||
# Entrypoint prepares the database. | ||
ENTRYPOINT ["/rails/bin/docker-entrypoint"] | ||
USER rails:rails | ||
|
||
# Start the server by default, this can be overwritten at runtime | ||
EXPOSE 3000 | ||
CMD ["./bin/rails", "server"] | ||
# Start the server | ||
CMD ["bin/rails", "server", "-b", "0.0.0.0"] |
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 was deleted.
Oops, something went wrong.
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,16 +1,29 @@ | ||
module Services | ||
class AuthService | ||
def initialize | ||
# TODO make this selectable | ||
@impl = AppRegistryService.new | ||
@domain_ownership_service = DomainOwnershipService.new | ||
end | ||
|
||
def authenticate!(token) | ||
@impl.authenticate!(token) | ||
identity = decode(token) | ||
raise AuthError unless identity | ||
# TODO verify identity with authority? | ||
identity | ||
end | ||
|
||
def authorize!(token, cert_issue_req) | ||
@impl.authorize!(token, cert_issue_req) | ||
def authorize!(identity, cert_issue_req) | ||
@domain_ownership_service.authorize!(identity, cert_issue_req) | ||
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] | ||
Identity.new(body) | ||
rescue => e | ||
Rails.logger.warn "Unable to decode token: #{e}" | ||
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module Services | ||
class DomainOwnershipService | ||
def authorize!(identity, cert_req) | ||
cert_req.fqdns.each do |fqdn| | ||
domain = get_domain_name(fqdn) | ||
raise AuthError unless domain.owner == identity.subject || | ||
(domain.group_delegation && | ||
(domain.groups & identity.groups).any?) | ||
end | ||
nil | ||
end | ||
|
||
private | ||
|
||
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,8 @@ | ||
class DomainInfo | ||
include ActiveModel::Model | ||
include ActiveModel::Attributes | ||
|
||
attribute :owner, :string | ||
attribute :groups, array: :string, default: [] | ||
attribute :group_delegation, :boolean, default: false | ||
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,13 @@ | ||
class Identity | ||
include ActiveModel::Model | ||
include ActiveModel::Attributes | ||
|
||
attribute :subject, :string | ||
attribute :name, :string | ||
attribute :iat, :integer | ||
attribute :aud, :string | ||
attribute :groups, array: :string, default: [] | ||
|
||
alias_attribute :sub, :subject | ||
alias_attribute :roles, :groups | ||
end |
Oops, something went wrong.