Skip to content

Commit

Permalink
Make the nix flake check tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamc authored and cole-h committed Nov 11, 2023
1 parent 9511afd commit d49cd33
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 14 deletions.
4 changes: 2 additions & 2 deletions book/src/admin-guide/deployment/nixos.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ Attic provides [a NixOS module](https://github.com/zhaofengli/attic/blob/main/ni
The RS256 JWT secret can be generated with the `openssl` utility:

```bash
openssl genrsa -traditional -out private_key.pem 4096
nix run nixpkgs#openssl -- genrsa -traditional -out - 4096 | base64 -w0
```

Create a file on the server containing the following contents:

```
ATTIC_SERVER_TOKEN_RS256_SECRET="output from openssl"
ATTIC_SERVER_TOKEN_RS256_SECRET="output from above"
```

Ensure the file is only accessible by root.
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/basic/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let
serverConfigFile = config.nodes.server.services.atticd.configFile;

cmd = {
atticadm = "atticd-atticadm";
atticadm = ". /etc/atticd.env && export ATTIC_SERVER_TOKEN_RS256_SECRET && atticd-atticadm";
atticd = ". /etc/atticd.env && export ATTIC_SERVER_TOKEN_RS256_SECRET && atticd -f ${serverConfigFile}";
};

Expand Down Expand Up @@ -125,7 +125,7 @@ in {

# For testing only - Don't actually do this
environment.etc."atticd.env".text = ''
ATTIC_SERVER_TOKEN_RS256_SECRET="$(openssl genrsa -traditional -out - 512)"
ATTIC_SERVER_TOKEN_RS256_SECRET='LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpNSUlFcEFJQkFBS0NBUUVBekhqUzFGKzlRaFFUdlJZYjZ0UGhxS09FME5VYkIraTJMOTByWVBNQVVoYVBUMmlKCmVUNk9vWFlmZWszZlZ1dXIrYks1VWFVRjhUbEx2Y1FHa1Arckd0WDRiQUpGTWJBcTF3Y25FQ3R6ZGVERHJnSlIKMGUvNWJh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vdVdKV0lBTHFDYm9zOTRRSVdPSG5HRUtSSGkydWIzR0d6U2tRSzN1eTUrdQo4M0txaFJOejRVMkdOK1pLaFE0NHhNVmV4TUVvZzJVU3lTaVZ0cFdqWXBwT2Q1NnVaMzRWaFU2TWRNZS9zT0JnCnNoei84MUxUSis2cHdFZE9wV2tPVlRaMXJISlZXQmdtVk5qWjc1c0NnWUVBNVd5YjBaU2dyMEVYTVRLa2NzNFcKTENudXV0cDZodEZtaWsrd29IZCtpOStMUThFSU1BdXVOUzJrbHJJYlAxVmhrWXkxQzZMNFJkRTV2M2ZyT05XUApmL3ZyYzdDTkhZREdacWlyVUswWldvdXB5b0pQLzBsOWFXdkJHT3hxSUZ2NDZ2M3ZvV1NNWkdBdFVOenpvaGZDClhOeks3WmF2dndka0JOT0tNQVQ5RU1FQ2dZRUF3NEhaWDRWNUo1d2dWVGVDQ2RjSzhsb2tBbFpBcUNZeEw5SUEKTjZ4STVUSVpSb0dNMXhXcC81dlRrci9rZkMwOU5YUExiclZYbVZPY1JrTzFKTStmZDhjYWN1OEdqck11dHdMaAoyMWVQR0N3cWlQMkZZZTlqZVFTRkZJU0hhZXpMZll3V2NSZmhvdURudGRxYXpaRHNuU0kvd1RMZXVCOVFxU0lRCnF0NzByczBDZ1lCQ2lzV0VKdXpQUUlJNzVTVkU4UnJFZGtUeUdhOEVBOHltcStMdDVLRDhPYk80Q2JHYVFlWXkKWFpjSHVyOFg2cW1lWHZVU3MwMHBMMUdnTlJ3WCtSUjNMVDhXTm9vc0NqVDlEUW9GOFZveEtseDROVTRoUGlrTQpBc0w1RS9wYnVLeXkvSU5LTnQyT3ZPZmJYVitlTXZQdGs5c1dORjNyRTBYcU15TW9maG9NaVE9PQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo='
'';

services.atticd = {
Expand Down
6 changes: 3 additions & 3 deletions nixos/atticd.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let
} ''
cat $configFile
export ATTIC_SERVER_TOKEN_RS256_SECRET="$(${pkgs.openssl}/bin/openssl genrsa -traditional -out - 512)"
export ATTIC_SERVER_TOKEN_RS256_SECRET="$(${pkgs.openssl}/bin/openssl genrsa -traditional -out - 1024 | ${pkgs.coreutils}/bin/base64 -w0)"
export ATTIC_SERVER_DATABASE_URL="sqlite://:memory:"
${cfg.package}/bin/atticd --mode check-config -f $configFile
cat <$configFile >$out
Expand Down Expand Up @@ -79,7 +79,7 @@ in
variables:
- ATTIC_SERVER_TOKEN_RS256_SECRET: The PEM-encoded version of the
RS256 JWT secret. Generate it with `openssl genrsa -traditional -out private_key.pem 4096`.
RS256 JWT secret. Generate it with `openssl genrsa -traditional -out - 4096 | base64 -w0`.
'';
type = types.nullOr types.path;
default = null;
Expand Down Expand Up @@ -135,7 +135,7 @@ in
message = ''
<option>services.atticd.credentialsFile</option> is not set.
Run `openssl genrsa -traditional -out private_key.pem 4096` and create a file with the following contents:
Run `openssl genrsa -traditional -out private_key.pem 4096 | base64 -w0` and create a file with the following contents:
ATTIC_SERVER_TOKEN_RS256_SECRET="output from command"
Expand Down
2 changes: 1 addition & 1 deletion server/src/config-template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ allowed-hosts = []

# JWT signing token
#
# Set this to the PEM encoding of some random data.
# Set this to the base64-encoded, then PEM encoding of an RSA key.
# You can also set it via the `ATTIC_SERVER_TOKEN_RS256_SECRET` environment
# variable.
token-rs256-secret = "%token_rs256_secret%"
Expand Down
2 changes: 1 addition & 1 deletion server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const XDG_PREFIX: &str = "attic";
/// This is useful for deploying to certain application platforms like Fly.io
const ENV_CONFIG_BASE64: &str = "ATTIC_SERVER_CONFIG_BASE64";

/// Environment variable storing the PEM-encoded RS256 JWT secret.
/// Environment variable storing the base64 encoded, then PEM-encoded RS256 JWT secret.
const ENV_TOKEN_RS256_SECRET: &str = "ATTIC_SERVER_TOKEN_RS256_SECRET";

/// Environment variable storing the database connection string.
Expand Down
2 changes: 1 addition & 1 deletion token/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ attic = { path = "../attic", default-features = false }
base64 = "0.21.2"
chrono = "0.4.24"
displaydoc = "0.2.4"
jsonwebtoken = "9.1.0"
jsonwebtoken = { version = "9.1.0", features = ["use_pem"] }
lazy_static = "1.4.0"
regex = "1.8.3"
serde = "1.0.163"
Expand Down
19 changes: 15 additions & 4 deletions token/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub use jsonwebtoken::{DecodingKey, EncodingKey};
use rsa::pkcs1::{DecodeRsaPrivateKey, EncodeRsaPublicKey};
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, BoolFromInt};
use base64::{engine::general_purpose::STANDARD as BASE64_STANDARD, Engine};

use attic::cache::{CacheName, CacheNamePattern};

Expand Down Expand Up @@ -272,8 +273,14 @@ pub enum Error {
/// JWT error: {0}
TokenError(jsonwebtoken::errors::Error),

/// Base64 decode error: {0}
Base64Error(base64::DecodeError),

/// RSA Key error: {0}
RsaKeyError(rsa::pkcs1::Error),

/// Failure decoding the base64 layer of the base64 encoded PEM
Utf8Error(std::str::Utf8Error)
}

impl Token {
Expand All @@ -285,7 +292,7 @@ impl Token {
validation.validate_nbf = true;
// validation.set_issuer(&[ctx.config.flakehub_jwt_bound_issuer.clone()]);
// validation.set_audience(&[ctx.config.jwt_bound_audience.clone()]);
validation.set_required_spec_claims(&["exp", "nbf", "aud", "iss", "sub"]);
//validation.set_required_spec_claims(&["exp", "nbf", "aud", "iss", "sub"]);

jsonwebtoken::decode::<JWTClaims<TokenClaims>>(token, key, &validation)
.map_err(Error::TokenError)
Expand Down Expand Up @@ -314,7 +321,8 @@ impl Token {

/// Encodes the token.
pub fn encode(&self, key: &jsonwebtoken::EncodingKey) -> Result<String> {
let header = jsonwebtoken::Header::default();
let mut header = jsonwebtoken::Header::default();
header.alg = Algorithm::RS256;
jsonwebtoken::encode(&header, &self.0, key).map_err(Error::TokenError)
}

Expand Down Expand Up @@ -420,14 +428,17 @@ impl CachePermission {

impl StdError for Error {}

pub fn decode_token_rs256_secret(secret: &str) -> Result<(EncodingKey, DecodingKey)> {
pub fn decode_token_rs256_secret(s: &str) -> Result<(EncodingKey, DecodingKey)> {
let decoded = BASE64_STANDARD.decode(s).map_err(Error::Base64Error)?;
let secret = std::str::from_utf8(&decoded).map_err(Error::Utf8Error)?;

let private_key = rsa::RsaPrivateKey::from_pkcs1_pem(secret).map_err(Error::RsaKeyError)?;
let public_key = private_key.to_public_key();
let public_pkcs1_pem = public_key
.to_pkcs1_pem(rsa::pkcs1::LineEnding::LF)
.map_err(Error::RsaKeyError)?;

let encoding_key = EncodingKey::from_rsa_pem(secret.as_bytes()).map_err(Error::TokenError)?;
let encoding_key = EncodingKey::from_rsa_pem(&secret.as_bytes()).map_err(Error::TokenError)?;
let decoding_key =
DecodingKey::from_rsa_pem(public_pkcs1_pem.as_bytes()).map_err(Error::TokenError)?;

Expand Down

0 comments on commit d49cd33

Please sign in to comment.