1
1
mod sensitive;
2
2
3
3
use crate :: { Config , HoustonProblem } ;
4
+ use regex:: Regex ;
4
5
use sensitive:: Sensitive ;
5
6
use serde:: { Deserialize , Serialize } ;
6
7
7
- use std:: fmt;
8
- use std:: fs;
9
8
use std:: path:: PathBuf ;
9
+ use std:: { fmt, fs, io} ;
10
10
11
11
/// Collects configuration related to a profile.
12
12
#[ derive( Debug , Serialize , Deserialize ) ]
@@ -51,14 +51,20 @@ impl Profile {
51
51
///
52
52
/// Takes an optional `profile` argument. Defaults to `"default"`.
53
53
pub fn get_api_key ( name : & str , config : & Config ) -> Result < String , HoustonProblem > {
54
- tracing:: debug!( APOLLO_KEY = ?mask_key( & config. override_api_key) ) ;
55
- match & config. override_api_key {
54
+ let api_key: Result < String , HoustonProblem > = match & config. override_api_key {
56
55
Some ( api_key) => Ok ( api_key. to_string ( ) ) ,
57
56
None => {
58
57
let opts = LoadOpts { sensitive : true } ;
59
- Ok ( Profile :: load ( name, config, opts) ?. sensitive . api_key )
58
+ let profile = Profile :: load ( name, config, opts) ?;
59
+ Ok ( profile. sensitive . api_key )
60
60
}
61
- }
61
+ } ;
62
+
63
+ let api_key = api_key?;
64
+
65
+ tracing:: debug!( "using API key {}" , mask_key( & api_key) ) ;
66
+
67
+ Ok ( api_key)
62
68
}
63
69
64
70
/// Saves configuration options for a specific profile to the file system,
@@ -89,7 +95,7 @@ impl Profile {
89
95
let dir = Profile :: dir ( name, config) ;
90
96
tracing:: debug!( dir = ?dir) ;
91
97
Ok ( fs:: remove_dir_all ( dir) . map_err ( |e| match e. kind ( ) {
92
- std :: io:: ErrorKind :: NotFound => HoustonProblem :: ProfileNotFound ( name. to_string ( ) ) ,
98
+ io:: ErrorKind :: NotFound => HoustonProblem :: ProfileNotFound ( name. to_string ( ) ) ,
93
99
_ => HoustonProblem :: IOError ( e) ,
94
100
} ) ?)
95
101
}
@@ -107,7 +113,7 @@ impl Profile {
107
113
let entry_path = entry?. path ( ) ;
108
114
if entry_path. is_dir ( ) {
109
115
let profile = entry_path. file_stem ( ) . unwrap ( ) ;
110
- tracing:: debug!( profile = ?profile) ;
116
+ tracing:: debug!( ?profile) ;
111
117
profiles. push ( profile. to_string_lossy ( ) . into_owned ( ) ) ;
112
118
}
113
119
}
@@ -122,18 +128,14 @@ impl fmt::Display for Profile {
122
128
}
123
129
}
124
130
125
- // Masks all but the first 4 and last 4 chars of a key with a set number of *
126
- // valid keys are all at least 22 chars. We don't care if invalid keys
131
+ /// Masks all but the first 4 and last 4 chars of a key with a set number of *
132
+ /// valid keys are all at least 22 chars.
133
+ // We don't care if invalid keys
127
134
// are printed, so we don't need to worry about strings 8 chars or less,
128
135
// which this fn would just print back out
129
- pub fn mask_key ( key : & Option < String > ) -> Option < String > {
130
- if let Some ( key) = key {
131
- let ex = regex:: Regex :: new ( r"(?im)^(.{4})(.*)(.{4})$" ) . unwrap ( ) ;
132
- let masked = ex. replace ( key, "$1******************$3" ) . into ( ) ;
133
- Some ( masked)
134
- } else {
135
- None
136
- }
136
+ pub fn mask_key ( key : & str ) -> String {
137
+ let ex = Regex :: new ( r"(?im)^(.{4})(.*)(.{4})$" ) . expect ( "Could not create regular expression." ) ;
138
+ ex. replace ( key, "$1******************$3" ) . to_string ( )
137
139
}
138
140
139
141
#[ cfg( test) ]
@@ -143,15 +145,15 @@ mod tests {
143
145
#[ test]
144
146
#[ allow( clippy:: many_single_char_names) ]
145
147
fn masks_valid_keys_properly ( ) {
146
- let a = Some ( "user:gh.foo:djru4788dhsg3657fhLOLO" . to_string ( ) ) ;
147
- assert_eq ! ( mask_key( & a) , Some ( "user******************LOLO" . to_string( ) ) ) ;
148
- let b = Some ( "service:foo:dh47dh27sg18aj49dkLOLO" . to_string ( ) ) ;
149
- assert_eq ! ( mask_key( & b) , Some ( "serv******************LOLO" . to_string( ) ) ) ;
150
- let c = Some ( "some nonsense" . to_string ( ) ) ;
151
- assert_eq ! ( mask_key( & c) , Some ( "some******************ense" . to_string( ) ) ) ;
152
- let d = Some ( "" . to_string ( ) ) ;
153
- assert_eq ! ( mask_key( & d) , Some ( "" . to_string( ) ) ) ;
154
- let e = Some ( "short" . to_string ( ) ) ;
155
- assert_eq ! ( mask_key( & e) , Some ( "short" . to_string( ) ) ) ;
148
+ let a = "user:gh.foo:djru4788dhsg3657fhLOLO" ;
149
+ assert_eq ! ( mask_key( a) , "user******************LOLO" . to_string( ) ) ;
150
+ let b = "service:foo:dh47dh27sg18aj49dkLOLO" ;
151
+ assert_eq ! ( mask_key( b) , "serv******************LOLO" . to_string( ) ) ;
152
+ let c = "some nonsense" ;
153
+ assert_eq ! ( mask_key( c) , "some******************ense" . to_string( ) ) ;
154
+ let d = "" ;
155
+ assert_eq ! ( mask_key( d) , "" . to_string( ) ) ;
156
+ let e = "short" ;
157
+ assert_eq ! ( mask_key( e) , "short" . to_string( ) ) ;
156
158
}
157
159
}
0 commit comments