Andrea Frittoli | 8283b4e | 2014-07-17 13:28:58 +0100 | [diff] [blame] | 1 | # Copyright (c) 2014 Hewlett-Packard Development Company, L.P. |
| 2 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | # you may not use this file except in compliance with the License. |
| 4 | # You may obtain a copy of the License at |
| 5 | # |
| 6 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | # |
| 8 | # Unless required by applicable law or agreed to in writing, software |
| 9 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | # See the License for the specific language governing permissions and |
| 12 | # limitations under the License. |
| 13 | |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 14 | import os |
| 15 | |
Andrea Frittoli | 8283b4e | 2014-07-17 13:28:58 +0100 | [diff] [blame] | 16 | from tempest.common import accounts |
Emily Hugenbruch | e7991d9 | 2014-12-12 16:53:36 +0000 | [diff] [blame] | 17 | from tempest.common import cred_provider |
Andrea Frittoli | 8283b4e | 2014-07-17 13:28:58 +0100 | [diff] [blame] | 18 | from tempest.common import isolated_creds |
| 19 | from tempest import config |
Matthew Treinish | fda3765 | 2015-02-20 15:14:53 -0500 | [diff] [blame] | 20 | from tempest import exceptions |
Andrea Frittoli | 8283b4e | 2014-07-17 13:28:58 +0100 | [diff] [blame] | 21 | |
| 22 | CONF = config.CONF |
| 23 | |
| 24 | |
| 25 | # Return the right implementation of CredentialProvider based on config |
| 26 | # Dropping interface and password, as they are never used anyways |
| 27 | # TODO(andreaf) Drop them from the CredentialsProvider interface completely |
| 28 | def get_isolated_credentials(name, network_resources=None, |
Andrea Frittoli (andreaf) | 345c7fa | 2015-03-25 10:10:42 -0400 | [diff] [blame] | 29 | force_tenant_isolation=False, |
| 30 | identity_version=None): |
Andrea Frittoli | 8283b4e | 2014-07-17 13:28:58 +0100 | [diff] [blame] | 31 | # If a test requires a new account to work, it can have it via forcing |
| 32 | # tenant isolation. A new account will be produced only for that test. |
| 33 | # In case admin credentials are not available for the account creation, |
| 34 | # the test should be skipped else it would fail. |
| 35 | if CONF.auth.allow_tenant_isolation or force_tenant_isolation: |
| 36 | return isolated_creds.IsolatedCreds( |
| 37 | name=name, |
Andrea Frittoli (andreaf) | 345c7fa | 2015-03-25 10:10:42 -0400 | [diff] [blame] | 38 | network_resources=network_resources, |
| 39 | identity_version=identity_version) |
Andrea Frittoli | 8283b4e | 2014-07-17 13:28:58 +0100 | [diff] [blame] | 40 | else: |
Aaron Rosen | 4807004 | 2015-03-30 16:17:11 -0700 | [diff] [blame] | 41 | if (CONF.auth.test_accounts_file and |
| 42 | os.path.isfile(CONF.auth.test_accounts_file)): |
Andrea Frittoli | 8283b4e | 2014-07-17 13:28:58 +0100 | [diff] [blame] | 43 | # Most params are not relevant for pre-created accounts |
Andrea Frittoli (andreaf) | 345c7fa | 2015-03-25 10:10:42 -0400 | [diff] [blame] | 44 | return accounts.Accounts(name=name, |
| 45 | identity_version=identity_version) |
Andrea Frittoli | 8283b4e | 2014-07-17 13:28:58 +0100 | [diff] [blame] | 46 | else: |
Andrea Frittoli (andreaf) | 345c7fa | 2015-03-25 10:10:42 -0400 | [diff] [blame] | 47 | return accounts.NotLockingAccounts( |
| 48 | name=name, identity_version=identity_version) |
Emily Hugenbruch | e7991d9 | 2014-12-12 16:53:36 +0000 | [diff] [blame] | 49 | |
| 50 | |
| 51 | # We want a helper function here to check and see if admin credentials |
| 52 | # are available so we can do a single call from skip_checks if admin |
| 53 | # creds area vailable. |
| 54 | def is_admin_available(): |
| 55 | is_admin = True |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 56 | # If tenant isolation is enabled admin will be available |
| 57 | if CONF.auth.allow_tenant_isolation: |
| 58 | return is_admin |
| 59 | # Check whether test accounts file has the admin specified or not |
Aaron Rosen | 4807004 | 2015-03-30 16:17:11 -0700 | [diff] [blame] | 60 | elif (CONF.auth.test_accounts_file and |
| 61 | os.path.isfile(CONF.auth.test_accounts_file)): |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 62 | check_accounts = accounts.Accounts(name='check_admin') |
| 63 | if not check_accounts.admin_available(): |
| 64 | is_admin = False |
Emily Hugenbruch | e7991d9 | 2014-12-12 16:53:36 +0000 | [diff] [blame] | 65 | else: |
| 66 | try: |
David Kranz | 8d557c7 | 2015-03-11 11:51:07 -0400 | [diff] [blame] | 67 | cred_provider.get_configured_credentials('identity_admin', |
| 68 | fill_in=False) |
Matthew Treinish | fda3765 | 2015-02-20 15:14:53 -0500 | [diff] [blame] | 69 | except exceptions.InvalidConfiguration: |
| 70 | is_admin = False |
Emily Hugenbruch | e7991d9 | 2014-12-12 16:53:36 +0000 | [diff] [blame] | 71 | return is_admin |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame^] | 72 | |
| 73 | |
| 74 | # We want a helper function here to check and see if alt credentials |
| 75 | # are available so we can do a single call from skip_checks if alt |
| 76 | # creds area vailable. |
| 77 | def is_alt_available(): |
| 78 | # If tenant isolation is enabled admin will be available |
| 79 | if CONF.auth.allow_tenant_isolation: |
| 80 | return True |
| 81 | # Check whether test accounts file has the admin specified or not |
| 82 | if (CONF.auth.test_accounts_file and |
| 83 | os.path.isfile(CONF.auth.test_accounts_file)): |
| 84 | check_accounts = accounts.Accounts(name='check_alt') |
| 85 | else: |
| 86 | check_accounts = accounts.NotLockingAccounts(name='check_alt') |
| 87 | try: |
| 88 | if not check_accounts.is_multi_user(): |
| 89 | return False |
| 90 | else: |
| 91 | return True |
| 92 | except exceptions.InvalidConfiguration: |
| 93 | return False |