Skip to content

Commit

Permalink
Add detailed panics so that we can see where failures occur (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmorley authored Jan 26, 2018
1 parent c53c6bb commit fcdc5d0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
23 changes: 14 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,39 +38,44 @@ struct Opt {
}

fn main() {
pretty_env_logger::init().unwrap();
pretty_env_logger::init()
.expect("Error initializing logger");

let opt = Opt::from_args();
debug!("Options: {:?}", opt);

let oktaws_config = config::fetch_config(&opt.profile).unwrap();
let oktaws_config = config::fetch_config(&opt.profile)
.expect("Error fetching config");

let (username, password) = credentials::get_credentials(opt.force_new);

let session_token = okta::login(&oktaws_config.organization, &username, &password)
.unwrap()
.expect("Error logging into Okta")
.session_token;
debug!("Session Token: {}", session_token);

let saml_assertion = okta::fetch_saml(
&oktaws_config.organization,
&oktaws_config.app_id,
&session_token,
).unwrap();
).expect("Error fetching SAML assertion from Okta");
debug!("SAML assertion: {}", saml_assertion);

let saml_attributes = aws::find_saml_attributes(&saml_assertion).unwrap();
let saml_attributes = aws::find_saml_attributes(&saml_assertion)
.expect("Error finding SAML attributes");
debug!("SAML attributes: {:?}", saml_attributes);

let principal_arn = saml_attributes.get(&oktaws_config.role).unwrap();
let principal_arn = saml_attributes.get(&oktaws_config.role)
.expect("Error getting the principal ARN from SAML attributes");
debug!("Principal ARN: {}", principal_arn);

let credentials = aws::assume_role(principal_arn, &oktaws_config.role, &saml_assertion)
.unwrap()
.expect("Error assuming role in AWS")
.credentials
.unwrap();
.expect("Error fetching credentials from assumed AWS role");
debug!("Credentials: {:?}", credentials);

aws::set_credentials(&opt.profile, &credentials).unwrap();
aws::set_credentials(&opt.profile, &credentials)
.expect("Error setting AWS credentials");
credentials::set_credentials(&username, &password);
}
26 changes: 16 additions & 10 deletions src/okta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,37 @@ use reqwest;
use scraper::{Html, Selector};

#[derive(Serialize)]
struct OktaLoginRequest {
username: String,
password: String,
#[serde(rename_all = "camelCase")]
struct LoginRequest {
username: Option<String>,
password: Option<String>,
relay_state: Option<String>,
token: Option<String>,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct OktaLoginResponse {
pub struct LoginResponse {
expires_at: String,
pub session_token: String,
status: String,
}

pub fn login(org: &str, user: &str, password: &str) -> Result<OktaLoginResponse, Error> {
let req = OktaLoginRequest {
username: String::from(user),
password: String::from(password),
pub fn login(org: &str, user: &str, password: &str) -> Result<LoginResponse, Error> {
let req = LoginRequest {
username: Some(String::from(user)),
password: Some(String::from(password)),
relay_state: None,
token: None,
};

let client = reqwest::Client::new();
Ok(client
let mut resp: reqwest::Response = client
.post(&format!("https://{}.okta.com/api/v1/authn", org))
.json(&req)
.send()?
.json()?)
.error_for_status()?;
Ok(resp.json()?)
}

pub fn fetch_saml(org: &str, app_id: &str, session_token: &str) -> Result<String, Error> {
Expand Down

0 comments on commit fcdc5d0

Please sign in to comment.