Skip to content
This repository has been archived by the owner on Dec 21, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2 from avvo/app-id-auth
Browse files Browse the repository at this point in the history
Add app-id authentication method
  • Loading branch information
jnewton-avvo authored Jan 3, 2018
2 parents aeca0e7 + 70df6f8 commit f54edf1
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "avvoenv"
version = "0.2.1"
version = "0.3.0"
authors = ["Avvo Infrastructure Team <[email protected]>"]
license = "MIT"

Expand Down
18 changes: 18 additions & 0 deletions avvoenv.1.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ avvoenv(1) -- fetch service environment variables
[`-F`|`--force`]
[`-I`|`--isolate`]
[`-i`|`--include` <pattern>]
[`-p`|`--app-id` <app-id>]
[`-r`|`--app-user` <app-user>]
[`-s`|`--service` <name>]
[`-t`|`--vault-token` <token>]
[`-u`|`--vault` <url>]
Expand All @@ -23,6 +25,8 @@ avvoenv(1) -- fetch service environment variables
[`-e`|`--exclude` <pattern>]
[`-f`|`--format` <format>]
[`-i`|`--include` <pattern>]
[`-p`|`--app-id` <app-id>]
[`-r`|`--app-user` <app-user>]
[`-s`|`--service` <name>]
[`-t`|`--vault-token` <token>]
[`-u`|`--vault` <url>]
Expand Down Expand Up @@ -92,6 +96,14 @@ current service, or canonicalise the name given with the `--service` option.
range eg `[0-9]` or `[a-z]`
`[!...]` is the inverse of `[...]`

* `-p`, `--app-id` <app-id>:
Authenticate with Vault via app-id. If the <app-id> argument is provided
it will override the `VAULT_APP_ID` environment variable.

* `-r`, `--app-user` <app-user>:
Set the user-id for use with Vault app-id authentication. If the <app-user>
argument is provided it will override the `VAULT_APP_USER` environment variable.

* `-s`, `--service` <name>:
Set the service name, overriding the `SERVICE` environment variable.
If neither `--service` or `SERVICE` are provided the `./requirements.yml`
Expand Down Expand Up @@ -161,6 +173,12 @@ outputs to standard output):
* `VAULT_ADDR`:
The Vault URL, overriding the default of <https://127.0.0.1:8200>.

* `VAULT_APP_ID`:
The application ID used with Vault app-id authentication.

* `VAULT_APP_USER`:
The application user ID used with Vault app-id authentication.

* `VAULT_TOKEN`:
The token used to authenticate with Vault, overriding the `~/.vault-token`
file.
Expand Down
10 changes: 10 additions & 0 deletions src/avvoenv/commands/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub fn add_fetch_opts(mut opts: getopts::Options) -> getopts::Options {
opts.optmulti("i", "include", "filter fetched variables", "PATTERN");
opts.optmulti("e", "exclude", "filter fetched variables", "PATTERN");
opts.optopt("t", "vault-token", "set the vault token", "TOKEN");
opts.optopt("r", "app-user", "authenticate with vault app-user", "VAULT_APP_USER");
opts.optopt("p", "app-id", "authenticate with vault app-id", "VAULT_APP_ID");
opts
}

Expand Down Expand Up @@ -62,6 +64,14 @@ pub fn env_from_opts(matches: &getopts::Matches) -> Result<Env, commands::Comman
if vault_client.ldap_auth(username, password).is_err() {
return Err(ErrorWithMessage(String::from("Authentication failed")));
};
} else if let Some(app_id) = opt_env(matches, "app-id", "VAULT_APP_ID") {
let app_user = match opt_env(matches, "app-user", "VAULT_APP_USER") {
Some(val) => val,
None => return Err(ErrorWithMessage(String::from("Could not determine app-user"))),
};
if vault_client.app_id_auth(app_id, app_user).is_err() {
return Err(ErrorWithMessage(String::from("Authentication failed")));
};
} else {
let mut path = std::env::home_dir().unwrap_or(std::path::PathBuf::from("/"));
path.push(".vault-token");
Expand Down
12 changes: 12 additions & 0 deletions src/avvoenv/source/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ pub struct AuthRequest {
pub password: String,
}

#[derive(Serialize)]
pub struct AuthAppIdRequest {
pub user_id: String,
}

#[derive(Deserialize)]
pub struct AuthResponse {
pub client_token: String,
Expand Down Expand Up @@ -75,6 +80,13 @@ impl Client {
Ok(())
}

pub fn app_id_auth(&mut self, app_id: String, user_id: String) -> Result<(), errors::Error> {
let request = AuthAppIdRequest { user_id };
let response: AuthResponseWrapper = self.post_json(&format!("auth/app-id/login/{}", app_id), &request)?;
self.token = Some(response.auth.client_token);
Ok(())
}

pub fn renew_token(&mut self) -> Result<(), errors::Error> {
let _:AuthResponseWrapper = self.post_json("/auth/token/renew-self", &TokenRenewRequest {})?;
Ok(())
Expand Down

0 comments on commit f54edf1

Please sign in to comment.