Skip to content
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 SSL support to astral api server #53

Merged
merged 14 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ USER rails:rails
# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
# Start the server
CMD ["bin/rails", "server", "-b", "0.0.0.0"]
CMD ["bin/http.sh"]
GeorgeJahad marked this conversation as resolved.
Show resolved Hide resolved
30 changes: 24 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,37 @@ file. Per-environment settings in the config file(development, test,
production) will override the shared values for that type.

## mTLS connections
Astral can connect to Vault with mTLS. Just
set the following values in `config/astral.yml`:
Astral can be run as an SSL service and can communicate with Vault via SSL.
Just set the following values in `config/astral.yml` (or environment) to
encrypt Astral-to-Vault :
```
vault_ssl_cert:
vault_ssl_client_cert:
vault_ssl_client_key:
```
A self-signed server cert for Vault can be generated with the following
command:

A self-signed server cert for Vault, Astral, and the OIDC provider can be
generated with the following command, and initial placeholder certs are already provided.
```
rake configure:ssl
```

To use SSL in the devcontainer, edit `.devcontainer/docker-compose.yml` so
that the `app` service has `VAULT_ADDRESS` of `https://vault:8443`.
To use Vault SSL in the devcontainer, edit
`.devcontainer/docker-compose.yml` so that the `app` service has
`VAULT_ADDRESS` of `https://vault:8443`. Client certs can also be
configured -- in which case Vault needs to be configured to verify
with a CA cert.


To use Astral with SSL in production, provide the necessary environment (SSL_CERT, SSL_KEY) to
the container environment, and use the `bin/ssl.sh` startup command. Eg:
```
docker run -p 3000:3000 \
-e SSL_CERT=/certs/cert.pem \
-e SSL_KEY=/certs/key.key \
-v certs:/certs:cached \
astral:latest bin/ssl.sh
```

## OIDC configuration
The OIDC modules allow the assignment of a policy to an OIDC user, by
Expand Down Expand Up @@ -123,6 +139,8 @@ config/astral.yml).
The rails test's configure the OIDC initial user, so if the tests pass,
you can invoke the oidc login as follows:

To use SSL in production, provide the necessary environment (SSL_CERT, SSL_KEY) to
the container environment, and use the `bin/ssl.sh` startup command. Eg:
```
export VAULT_ADDR=http://127.0.0.1:8200; vault login -method=oidc
```
Expand Down
2 changes: 2 additions & 0 deletions bin/http.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#! /bin/sh
bin/rails s -b 0.0.0.0
5 changes: 5 additions & 0 deletions bin/ssl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! /bin/sh
SSL_CERT="${SSL_CERT:-cert/astral.pem}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we call these "ASTRAL_SSL_CERT/KEY" just to distinguish them from the other env vars?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, will rename

SSL_KEY="${SSL_KEY:-cert/astral.key}"

bin/rails s -b "ssl://0.0.0.0:3000?key=${SSL_KEY}&cert=${SSL_CERT}"
41 changes: 27 additions & 14 deletions lib/tasks/configure.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,35 @@ require "rake"

# Rake tasks for making a vault cert
namespace :configure do
desc "Make Vault, Astral, and OIDC provider certs"
task ssl: [ :vault_ssl, :astral_ssl, :oidc_provider_ssl ]

desc "Make the server cert for vault"
task :ssl, [ :cert_name ] do |t, args|
cert_name = args[:cert_name]
cert_name = "vault" if cert_name.nil?
sanParam = "subjectAltName=DNS:#{cert_name}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sanParam is actually needed for the oidc provider cert. I mentioned it in my PR here: #49 (comment)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will restore -- I assume can we use this in all cases?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume can we use this in all cases?

That is my understanding. I did recreate the vault cert with it, and all the tests still pass with the address set like so: "VAULT_ADDR=https://vault:8443"

%x(
openssl req -new -newkey rsa:4096 -nodes \
-keyout cert/#{cert_name}.key -out cert/#{cert_name}.csr \
-subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=#{cert_name}" \
-addext #{sanParam} \
task :vault_ssl do
keygen("vault")
end

desc "Make the server cert for astral"
task :astral_ssl do
keygen("astral")
end

echo #{sanParam} > /tmp/sanParam
openssl x509 -req -days 365 -in cert/#{cert_name}.csr \
-signkey cert/#{cert_name}.key \
-out cert/#{cert_name}.pem -extfile /tmp/sanParam
desc "Make the server cert for the oidc provider"
task :oidc_provider_ssl do
keygen("oidc_provider")
end

private

def keygen(name)
%x(
openssl req -new -newkey rsa:4096 -nodes \
-keyout cert/#{name}.key -out cert/#{name}.csr \
-subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=#{name}"
openssl x509 -req -days 365 -in cert/#{name}.csr \
-signkey cert/#{name}.key \
-out cert/#{name}.pem
)
puts "SSL key for #{cert_name} created"
puts "SSL key for #{name} created"
end
end