@@ -115,30 +115,34 @@ def s3_from_env(creds, fieldnm, env, prefix=""):
115115
116116
117117def s3_from_ostack (creds , conn , endpoint ):
118- "Set creds from openstack swift/keystone"
118+ """Set creds from openstack swift/keystone
119+ Returns credential ID *if* an ec2 credential was created,
120+ None otherwise."""
119121 rgx = re .compile (r"^(https*://[^/]*)/" )
120122 match = rgx .match (endpoint )
121123 if match :
122124 creds ["HOST" ] = match .group (1 )
123- # Use first ec2 cred if one exists
125+ # Use first ec2 cred that matches the project (if one exists)
126+ project_id = conn .identity .get_project_id ()
124127 ec2_creds = [cred for cred in conn .identity .credentials ()
125- if cred .type == "ec2" ]
128+ if cred .type == "ec2" and cred . project_id == project_id ]
126129 if len (ec2_creds ):
127130 # FIXME: Assume cloud is not evil
128131 ec2_dict = eval (ec2_creds [0 ].blob , {"null" : None })
129132 creds ["AK" ] = ec2_dict ["access" ]
130133 creds ["SK" ] = ec2_dict ["secret" ]
131- return
134+ return None
132135 # Generate keyid and secret
133136 ak = uuid .uuid4 ().hex
134137 sk = uuid .uuid4 ().hex
135138 blob = f'{{"access": "{ ak } ", "secret": "{ sk } "}}'
136139 try :
137- conn .identity .create_credential (type = "ec2" , blob = blob ,
138- user_id = conn .current_user_id ,
139- project_id = conn .current_project_id )
140+ crd = conn .identity .create_credential (type = "ec2" , blob = blob ,
141+ user_id = conn .current_user_id ,
142+ project_id = conn .current_project_id )
140143 creds ["AK" ] = ak
141144 creds ["SK" ] = sk
145+ return crd .id
142146 except BaseException as exc :
143147 print (f"WARNING: ec2 creds creation failed: { exc !s} " , file = sys .stderr )
144148 # pass
@@ -173,7 +177,7 @@ def check_for_s3_and_swift(conn: openstack.connection.Connection, s3_credentials
173177 )
174178 return 1
175179 # Get S3 endpoint (swift) and ec2 creds from OpenStack (keystone)
176- s3_from_ostack (s3_creds , conn , endpoint )
180+ ec2_cred = s3_from_ostack (s3_creds , conn , endpoint )
177181 # Overrides (var names are from libs3, in case you wonder)
178182 s3_from_env (s3_creds , "HOST" , "S3_HOSTNAME" , "https://" )
179183 s3_from_env (s3_creds , "AK" , "S3_ACCESS_KEY_ID" )
@@ -190,14 +194,19 @@ def check_for_s3_and_swift(conn: openstack.connection.Connection, s3_credentials
190194 # if not swift_containers:
191195 # swift_containers = create_container(conn, TESTCONTNAME)
192196 result = 0
197+ # Compare number of buckets/containers
198+ # FIXME: Could compare list of sorted names
193199 if Counter (s3_buckets ) != Counter (swift_containers ):
194200 logger .warning ("S3 buckets and Swift Containers differ:\n "
195201 f"S3: { sorted (s3_buckets )} \n SW: { sorted (swift_containers )} " )
196202 result = 1
197203 else :
198204 logger .info ("SUCCESS: S3 and Swift exist and agree" )
199- # Clean up
200- # FIXME: Cleanup created EC2 credential
205+ # Clean up ec2 cred IF we created one
206+ if ec2_cred :
207+ conn .identity .delete_credential (ec2_cred )
208+ # No need to clean up swift container, as we did not create one
209+ # (If swift and S3 agree, there will be a S3 bucket that we clean up with S3.)
201210 # if swift_containers == [TESTCONTNAME]:
202211 # del_container(conn, TESTCONTNAME)
203212 # Cleanup created S3 bucket
0 commit comments