1616 get_secrets_from_index ,
1717 update_index_secret ,
1818 validate_collection ,
19- validate_secret_name ,
19+ validate_path ,
2020 validate_secret_source ,
21+ ensure_authentication ,
2122)
2223
2324# Metadata keys used when creating secrets:
4142 type = str ,
4243 callback = validate_collection ,
4344)
44- @click .option (
45- "-s" ,
46- "--secret" ,
47- required = True ,
48- help = "Name of the secret." ,
49- type = str ,
50- callback = validate_secret_name ,
51- )
45+ @click .argument ("path" , required = True , callback = validate_path )
5246@click .option (
5347 "-f" ,
5448 "--from-file" ,
5953@click .option (
6054 "-l" , "--from-literal" , default = "" , help = "Secret data as string input." , type = str
6155)
62- def create (collection : str , secret : str , from_file : str , from_literal : str ):
63- """Create a new secret in the specified collection."""
56+ def create (collection : str , path : str , from_file : str , from_literal : str ):
57+ """Create a new secret in the specified collection.
58+
59+ The secret PATH should be in the format 'group/field' where:
60+ - group: Organizes related secrets (can be hierarchical: 'aws/prod')
61+ - field: The specific secret name (e.g., 'username', 'password')
6462
63+ Example: secret-manager create -c my-collection aws/password -l "secret value"
64+ """
65+ ensure_authentication ()
6566 validate_secret_source (from_file , from_literal )
6667
6768 if not check_if_collection_exists (collection ):
@@ -71,15 +72,20 @@ def create(collection: str, secret: str, from_file: str, from_literal: str):
7172 "See: https://docs.ci.openshift.org/docs/how-tos/adding-a-new-secret-to-ci/"
7273 )
7374 client = secretmanager .SecretManagerServiceClient ()
74- secret_name = get_secret_name (collection , secret )
7575
76- # Check if secret exists in either index or GCP
7776 index_secrets = get_secrets_from_index (client , collection )
78- if secret in index_secrets :
79- raise click .ClickException (f"Secret named '{ secret } ' already exists." )
77+ path_normalized = path .replace ("/" , "__" )
78+ if path_normalized in index_secrets :
79+ raise click .ClickException (
80+ f"Secret '{ path } ' already exists."
81+ )
82+
83+ secret_id_normalized = get_secret_name (collection , path )
8084 try :
81- client .get_secret (name = client .secret_path (PROJECT_ID , secret_name ))
82- raise click .ClickException (f"Secret named '{ secret } ' already exists." )
85+ client .get_secret (name = client .secret_path (PROJECT_ID , secret_id_normalized ))
86+ raise click .ClickException (
87+ f"Secret '{ path } ' already exists."
88+ )
8389 except NotFound :
8490 pass # Secret doesn't exist in GCP - this is good
8591
@@ -95,7 +101,7 @@ def create(collection: str, secret: str, from_file: str, from_literal: str):
95101 gcp_secret = client .create_secret (
96102 request = {
97103 "parent" : f"projects/{ PROJECT_ID } " ,
98- "secret_id" : secret_name ,
104+ "secret_id" : secret_id_normalized ,
99105 "secret" : {
100106 "replication" : {"automatic" : {}},
101107 "labels" : labels ,
@@ -107,12 +113,12 @@ def create(collection: str, secret: str, from_file: str, from_literal: str):
107113 parent = gcp_secret .name ,
108114 payload = SecretPayload (data = create_payload (from_file , from_literal )),
109115 )
110- update_index_secret (client , collection , index_secrets + [secret ])
111- click .echo (f"Secret '{ secret } ' created" )
116+ update_index_secret (client , collection , index_secrets + [path_normalized ])
117+ click .echo (f"Secret '{ path } ' created successfully. " )
112118 except Exception as e :
113119 raise click .ClickException (
114- f"Failed to create secret '{ secret } ': { e } . "
115- f"If the secret is in an inconsistent state, run 'delete -c { collection } -s { secret } ', then try again."
120+ f"Failed to create secret '{ path } ': { e } . "
121+ f"If the secret is in an inconsistent state, run 'delete -c { collection } { path } ', then try again."
116122 ) from e
117123
118124
0 commit comments