Debian Codegraph @

octavia_tempest_plugin/common/barbican_client_mgr.py

raw symbols SCIP
1 # Copyright 2019 Rackspace US Inc. All rights reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 from barbicanclient import client
16 from keystoneauth1 import identity
17 from keystoneauth1 import session
18 from oslo_log import log as logging
19 from tempest.lib.common.utils import data_utils
20
21 LOG = logging.getLogger(__name__)
22
23
24 class BarbicanClientManager(object):
25 """Class for interacting with the barbican service.
26
27 This class is an abstraction for interacting with the barbican service.
28 This class currently uses the barbican client code to access barbican due
29 to the following reasons:
30 1. Octavia users typically load secrets into barbican via the client.
31 2. The barbican-tempest-plugin is lightly tested (no py3 tests, etc.).
32 3. barbican-tempest-plugin is not in global requirements.
33
34 This led to the decision to not use the service client in the
35 barbican-tempest-plugin.
36
37 In the future it may be better to use the barbican-tempest-plugin
38 service client or the openstacksdk.
39 """
40
41 def __init__(self, tempest_client_mgr):
42 """Setup the barbican client.
43
44 :param tempest_client_mgr: A tempest client manager object, such as
45 os_primary.
46 """
47 # Convert the tempest credential passed in into a keystone session
48 auth_provider = tempest_client_mgr.auth_provider
49 cert_validation = False
50 if not auth_provider.dscv:
51 cert_validation = auth_provider.ca_certs
52 credentials = tempest_client_mgr.credentials
53 keystone_auth = identity.v3.Token(
54 auth_url=auth_provider.auth_url,
55 token=auth_provider.get_token(),
56 project_id=credentials.project_id,
57 project_name=credentials.project_name,
58 project_domain_id=credentials.project_domain_id,
59 project_domain_name=credentials.project_domain_name)
60 id_session = session.Session(auth=keystone_auth,
61 verify=cert_validation)
62
63 # Setup the barbican client
64 self.barbican = client.Client(session=id_session)
65
66 def store_secret(self, secret):
67 """Store a secret in barbican.
68
69 :param secret: A pkcs12 secret.
70 :returns: The barbican secret_ref.
71 """
72 p12_secret = self.barbican.secrets.create()
73 p12_secret.name = data_utils.rand_name("lb_member_barbican")
74 p12_secret.payload = secret
75 secret_ref = p12_secret.store()
76 LOG.debug('Secret %s has ref %s', p12_secret.name, secret_ref)
77 return secret_ref
78
79 def delete_secret(self, secret_ref):
80 self.barbican.secrets.delete(secret_ref)
81
82 def add_acl(self, secret_ref, user_id):
83 acl_entity = self.barbican.acls.create(entity_ref=secret_ref,
84 users=[user_id],
85 project_access=True)
86 acl_ref = acl_entity.submit()
87 LOG.debug('Secret ACL %s added user %s', acl_ref, user_id)
88 return acl_ref