1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
mod config;
mod helper;

use std::path::{Path, PathBuf};
use std::env;
use std::str;
use base64;
use std::fmt;
use std::error::Error;

type Result<T> = std::result::Result<T, CredentialRetrievalError>;

/// An error that occurred whilst attempting to retrieve a credential.
#[derive(Debug, PartialEq)]
pub enum CredentialRetrievalError {
    HelperCommunicationError,
    MalformedHelperResponse,
    HelperFailure,
    CredentialDecodingError,
    NoCredentialConfigured,
    ConfigNotFound,
    ConfigReadError,
}

impl fmt::Display for CredentialRetrievalError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let message = match self {
            CredentialRetrievalError::HelperCommunicationError => "Unable to communicate with credential helper",
            CredentialRetrievalError::MalformedHelperResponse => "Credential helper response malformed",
            CredentialRetrievalError::HelperFailure => "Credential helper returned non-zero response code",
            CredentialRetrievalError::CredentialDecodingError => "Unable to decode credential",
            CredentialRetrievalError::NoCredentialConfigured => "User has no credential configured",
            CredentialRetrievalError::ConfigNotFound => "No config file found",
            CredentialRetrievalError::ConfigReadError => "Unable to read config",

        };
        write!(f, "{}", message)
    }
}

impl Error for CredentialRetrievalError {}

/// A docker credential, either a single identity token or a username/password pair.
#[derive(Debug, PartialEq)]
pub enum DockerCredential {
    IdentityToken(String),
    UsernamePassword(String, String)
}

fn config_dir() -> Option<PathBuf> {
    let home_config = || env::var_os("HOME").map(|home| Path::new(&home).join(".docker"));
    env::var_os("DOCKER_CONFIG")
        .map(|dir| Path::new(&dir).to_path_buf())
        .or_else(home_config)
}

fn decode_auth(encoded_auth: &str) -> Result<DockerCredential> {
    let decoded = base64::decode(encoded_auth).map_err(|_| CredentialRetrievalError::CredentialDecodingError)?;
    let decoded = str::from_utf8(&decoded).map_err(|_| CredentialRetrievalError::CredentialDecodingError)?;
    let parts: Vec<&str> = decoded.splitn(2, ':').collect();
    let username = String::from(*parts.get(0).unwrap());
    let password = String::from(*parts.get(1).ok_or(CredentialRetrievalError::CredentialDecodingError)?);
    Ok(DockerCredential::UsernamePassword(username, password))
}

fn extract_credential<T>(conf: config::DockerConfig, server: &str, from_helper: T) -> Result<DockerCredential>
    where T: Fn(&str, &str) -> Result<DockerCredential>

{
    if let Some(helpers) = conf.cred_helpers {
        if let Some(helper_name) = helpers.get(server) {
            return from_helper(server, helper_name);
        }
    }

    if let Some(auths) = conf.auths {
        if let Some(auth_config) = auths.get(server) {
            if let Some(auth) = &auth_config.auth {
                return Ok(decode_auth(auth)?);
            }
        }
    }

    if let Some(store_name) = conf.creds_store {
        return from_helper(server, &store_name);
    }

    Err(CredentialRetrievalError::NoCredentialConfigured)
}

/// Retrieve a user's docker credential via config.json.
///
/// If necessary, credential helpers/store will be invoked.
///
/// Example:
/// ```no_run
/// use docker_credential::DockerCredential;
///
/// let credential = docker_credential::get_credential("https://index.docker.io/v1/").expect("Unable to retrieve credential");
///
/// match credential {
///   DockerCredential::IdentityToken(token) => println!("Identity token: {}", token),
///   DockerCredential::UsernamePassword(user_name, password) => println!("Username: {}, Password: {}", user_name, password),
/// };
/// ```
pub fn get_credential(server: &str) -> Result<DockerCredential> {
    let dir = config_dir().ok_or(CredentialRetrievalError::ConfigNotFound)?;
    let conf = config::read_config(&dir)?;
    extract_credential(conf, server, helper::credential_from_helper)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;

    #[test]
    fn errors_when_no_relevant_config() {
        let empty_config = config::DockerConfig {
            auths: None,
            creds_store: None,
            cred_helpers: None,
        };
        let dummy_helper = |_: &str, _: &str| Err(CredentialRetrievalError::HelperCommunicationError);
        let result = extract_credential(empty_config, "some server", dummy_helper);

        assert_eq!(result, Err(CredentialRetrievalError::NoCredentialConfigured));
    }

    #[test]
    fn decodes_auth_when_no_helpers() {
        let encoded_auth = base64::encode("some_user:some_password");
        let mut auths = HashMap::new();
        auths.insert(String::from("some server"), config::AuthConfig { auth: Some(String::from(encoded_auth)) });
        let auth_config = config::DockerConfig {
            auths: Some(auths),
            creds_store: None,
            cred_helpers: None,
        };
        let dummy_helper = |_: &str, _: &str| Err(CredentialRetrievalError::HelperCommunicationError);
        let result = extract_credential(auth_config, "some server", dummy_helper);

        assert_eq!(result, Ok(DockerCredential::UsernamePassword(String::from("some_user"), String::from("some_password"))));
    }

    #[test]
    fn gets_credential_from_helper() {
        let mut helpers = HashMap::new();
        helpers.insert(String::from("some server"), String::from("some_helper"));
        let helper_config = config::DockerConfig {
            auths: None,
            creds_store: None,
            cred_helpers: Some(helpers),
        };
        let dummy_helper = |address: &str, helper: &str| {
            if address == String::from("some server") && helper == String::from("some_helper") {
                Ok(DockerCredential::IdentityToken(String::from("expected_token")))
            } else {
                Err(CredentialRetrievalError::HelperCommunicationError)
            }
        };
        let result = extract_credential(helper_config, "some server", dummy_helper);

        assert_eq!(result, Ok(DockerCredential::IdentityToken(String::from("expected_token"))));
    }

    #[test]
    fn gets_credential_from_store() {
        let store_config = config::DockerConfig {
            auths: None,
            creds_store: Some(String::from("cred_store")),
            cred_helpers: None,
        };
        let dummy_helper = |address: &str, helper: &str| {
            if address == String::from("some server") && helper == String::from("cred_store") {
                Ok(DockerCredential::IdentityToken(String::from("expected_token")))
            } else {
                Err(CredentialRetrievalError::HelperCommunicationError)
            }
        };
        let result = extract_credential(store_config, "some server", dummy_helper);

        assert_eq!(result, Ok(DockerCredential::IdentityToken(String::from("expected_token"))));

    }
}