Allow user to specify token as env var

This change is going to be done in conjunction with Pegleg's shipyard
helper code [0] such that an auth token can be set as an environment
variable.  If present it will be used rather than authenticating
against Keystone.  This is a requested change by Darren Dejaeger so
that when using automation such as Jenkins pipelines the user is not
forced to provide their credentials to have shipyard automatically
obtain the token - instead they can obtain their token separately and
provide that token for shipyard to use.

[0]: https://opendev.org/airship/pegleg/src/branch/master/pegleg/engine/util/shipyard_helper.py

Change-Id: I859bff978069e8cc3502b952273237e84de83456
This commit is contained in:
HUGHES, ALEXANDER (ah8742) 2019-06-19 12:04:59 -05:00 committed by Alexander Hughes
parent d1d945951c
commit 4594b8a719
1 changed files with 10 additions and 3 deletions

View File

@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import abc import abc
import os
import logging import logging
from keystoneauth1.exceptions.auth import AuthorizationFailure from keystoneauth1.exceptions.auth import AuthorizationFailure
@ -135,10 +136,16 @@ class BaseClient(metaclass=abc.ABCMeta):
raise ClientError(str(e)) raise ClientError(str(e))
def get_token(self): def get_token(self):
"""Returns the simple token string for a token
Attempt to read token from environment variable, if present use it.
If not, return the token obtained from Keystone.
""" """
Returns the simple token string for a token acquired from keystone token = os.environ.get('OS_AUTH_TOKEN')
""" if token:
return self._get_ks_session().get_auth_headers().get('X-Auth-Token') return token
else:
return self._get_ks_session().get_auth_headers().get('X-Auth-Token')
def _get_ks_session(self): def _get_ks_session(self):
self.logger.debug('Accessing keystone for keystone session') self.logger.debug('Accessing keystone for keystone session')