@@ -25,9 +25,13 @@ impl Store {
2525 ) ?,
2626 } ;
2727
28- let credentials_file = fs:: read_to_string ( & path) ?. parse ( ) . wrap_err_with ( || {
29- format ! ( "Failed to parse AWS credentials file {}" , & path. display( ) )
30- } ) ?;
28+ let credentials_file = if path. exists ( ) {
29+ fs:: read_to_string ( & path) ?. parse ( ) . wrap_err_with ( || {
30+ format ! ( "Failed to parse AWS credentials file {}" , & path. display( ) )
31+ } ) ?
32+ } else {
33+ AwsCredentialsFile :: default ( )
34+ } ;
3135
3236 Ok ( Self {
3337 path,
@@ -112,7 +116,13 @@ aws_secret_access_key = STATIC_SECRET_ACCESS_KEY
112116"# ;
113117
114118 #[ test]
115- fn insert_credential_no_file ( ) -> Result < ( ) > {
119+ fn load_no_file ( ) -> Result < ( ) > {
120+ Store :: load ( Some ( & PathBuf :: from ( "THIS PATH DOES NOT EXIST" ) ) ) ?;
121+ Ok ( ( ) )
122+ }
123+
124+ #[ test]
125+ fn insert_credential_empty_file ( ) -> Result < ( ) > {
116126 let tempfile = NamedTempFile :: new ( ) ?;
117127
118128 let mut store = Store :: load ( Some ( tempfile. path ( ) ) ) ?;
@@ -128,6 +138,17 @@ aws_secret_access_key = STATIC_SECRET_ACCESS_KEY
128138 ) ,
129139 ) ?;
130140
141+ store. upsert_credential (
142+ "bar" ,
143+ & Credentials :: new (
144+ "NEW_BAR_ACCESS_KEY" ,
145+ "NEW_BAR_SECRET_ACCESS_KEY" ,
146+ Some ( "NEW_BAR_SESSION_TOKEN" . to_string ( ) ) ,
147+ None ,
148+ "oktaws" ,
149+ ) ,
150+ ) ?;
151+
131152 store. save ( ) ?;
132153
133154 let contents = fs:: read_to_string ( tempfile) ?;
@@ -144,6 +165,16 @@ aws_secret_access_key = STATIC_SECRET_ACCESS_KEY
144165 lines. next( ) ,
145166 Some ( "aws_session_token = NEW_FOO_SESSION_TOKEN" )
146167 ) ;
168+ assert_eq ! ( lines. next( ) , Some ( "[bar]" ) ) ;
169+ assert_eq ! ( lines. next( ) , Some ( "aws_access_key_id = NEW_BAR_ACCESS_KEY" ) ) ;
170+ assert_eq ! (
171+ lines. next( ) ,
172+ Some ( "aws_secret_access_key = NEW_BAR_SECRET_ACCESS_KEY" )
173+ ) ;
174+ assert_eq ! (
175+ lines. next( ) ,
176+ Some ( "aws_session_token = NEW_BAR_SESSION_TOKEN" )
177+ ) ;
147178 assert_eq ! ( lines. next( ) , None ) ;
148179
149180 Ok ( ( ) )
@@ -264,7 +295,7 @@ aws_secret_access_key = STATIC_SECRET_ACCESS_KEY"
264295 "The credentials for static are not STS. Refusing to overwrite them
265296
266297Location:
267- {}:53 :24" ,
298+ {}:57 :24" ,
268299 PathBuf :: from_iter( [ "src" , "aws" , "profile.rs" ] ) . display( )
269300 ) ,
270301 ) ;
@@ -304,7 +335,7 @@ Caused by:
304335 1: Parsing Error: VerboseError {{ errors: [(\" foo\" , Nom(Eof))] }}
305336
306337Location:
307- {}:28:67 " ,
338+ {}:29:48 " ,
308339 tempfile. path( ) . display( ) ,
309340 PathBuf :: from_iter( [ "src" , "aws" , "profile.rs" ] ) . display( )
310341 )
0 commit comments