| 1 | # | ||||||||||||
| 2 | # Copyright (c) 2019 cPanel, L.L.C. | ||||||||||||
| 3 | # All rights reserved. | ||||||||||||
| 4 | # http://cpanel.net/ | ||||||||||||
| 5 | # | ||||||||||||
| 6 | # Distributed under the terms of the MIT license. See the LICENSE file for | ||||||||||||
| 7 | # further details. | ||||||||||||
| 8 | # | ||||||||||||
| 9 | package OpenStack::Client::Auth; | ||||||||||||
| 10 | |||||||||||||
| 11 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | 1 | ▶ | use OpenStack::Client (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | =encoding utf8 | ||||||||||||
| 17 | |||||||||||||
| 18 | =head1 NAME | ||||||||||||
| 19 | |||||||||||||
| 20 | OpenStack::Client::Auth - OpenStack Keystone authentication and authorization | ||||||||||||
| 21 | |||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||
| 23 | |||||||||||||
| 24 | use OpenStack::Client::Auth (); | ||||||||||||
| 25 | |||||||||||||
| 26 | my $auth = OpenStack::Client::Auth->new('http://openstack.foo.bar:5000/v2.0', | ||||||||||||
| 27 | 'tenant' => $ENV{'OS_TENANT_NAME'}, | ||||||||||||
| 28 | 'username' => $ENV{'OS_USERNAME'}, | ||||||||||||
| 29 | 'password' => $ENV{'OS_PASSWORD'} | ||||||||||||
| 30 | ); | ||||||||||||
| 31 | |||||||||||||
| 32 | # or you can also use API v3 | ||||||||||||
| 33 | $auth = OpenStack::Client::Auth->new( | ||||||||||||
| 34 | $ENV{OS_AUTH_URL}, | ||||||||||||
| 35 | 'username' => $ENV{'OS_USERNAME'}, | ||||||||||||
| 36 | 'password' => $ENV{'OS_PASSWORD'}, | ||||||||||||
| 37 | 'version' => 3, | ||||||||||||
| 38 | # provide a scope to get a catalog | ||||||||||||
| 39 | 'scope' => { | ||||||||||||
| 40 | project => { | ||||||||||||
| 41 | name => $ENV{'OS_PROJECT_NAME'}, | ||||||||||||
| 42 | domain => { id => 'default' }, | ||||||||||||
| 43 | } | ||||||||||||
| 44 | } | ||||||||||||
| 45 | ); | ||||||||||||
| 46 | |||||||||||||
| 47 | my $glance = $auth->service('image', | ||||||||||||
| 48 | 'region' => $ENV{'OS_REGION_NAME'} | ||||||||||||
| 49 | ); | ||||||||||||
| 50 | |||||||||||||
| 51 | =head1 DESCRIPTION | ||||||||||||
| 52 | |||||||||||||
| 53 | C<OpenStack::Client::Auth> provides an interface for obtaining authorization | ||||||||||||
| 54 | to access other OpenStack cloud services. | ||||||||||||
| 55 | |||||||||||||
| 56 | =head1 AUTHORIZING WITH KEYSTONE | ||||||||||||
| 57 | |||||||||||||
| 58 | =over | ||||||||||||
| 59 | |||||||||||||
| 60 | =item C<OpenStack::Client::Auth-E<gt>new(I<$endpoint>, I<%args>)> | ||||||||||||
| 61 | |||||||||||||
| 62 | Contact the OpenStack Keystone API at the address provided in I<$endpoint>, and | ||||||||||||
| 63 | obtain an authorization token and set of endpoints for which the client is | ||||||||||||
| 64 | allowed to access. Credentials are specified in I<%args>; the following named | ||||||||||||
| 65 | values are required: | ||||||||||||
| 66 | |||||||||||||
| 67 | =over | ||||||||||||
| 68 | |||||||||||||
| 69 | =item * B<tenant> | ||||||||||||
| 70 | |||||||||||||
| 71 | The OpenStack tenant (project) name | ||||||||||||
| 72 | |||||||||||||
| 73 | =item * B<username> | ||||||||||||
| 74 | |||||||||||||
| 75 | The OpenStack user name | ||||||||||||
| 76 | |||||||||||||
| 77 | =item * B<password> | ||||||||||||
| 78 | |||||||||||||
| 79 | The OpenStack password | ||||||||||||
| 80 | |||||||||||||
| 81 | =item * B<version> | ||||||||||||
| 82 | |||||||||||||
| 83 | The version of the Glance API to negotiate with. Default is C<2.0>, but | ||||||||||||
| 84 | C<3> is also accepted. | ||||||||||||
| 85 | |||||||||||||
| 86 | =item * B<scope> | ||||||||||||
| 87 | |||||||||||||
| 88 | When negotiating with an Identity v3 endpoint, the information provided here | ||||||||||||
| 89 | is passed in the B<scope> property of the B<auth> portion of the request body | ||||||||||||
| 90 | submitted to the endpoint. | ||||||||||||
| 91 | |||||||||||||
| 92 | =item * B<domain> | ||||||||||||
| 93 | |||||||||||||
| 94 | When negotiating with an Identity v3 endpoint, the name of the domain to | ||||||||||||
| 95 | authenticate to. | ||||||||||||
| 96 | |||||||||||||
| 97 | =back | ||||||||||||
| 98 | |||||||||||||
| 99 | When successful, this method will return an object containing the following: | ||||||||||||
| 100 | |||||||||||||
| 101 | =over | ||||||||||||
| 102 | |||||||||||||
| 103 | =item * response | ||||||||||||
| 104 | |||||||||||||
| 105 | The full decoded JSON authorization response from Keystone | ||||||||||||
| 106 | |||||||||||||
| 107 | =item * services | ||||||||||||
| 108 | |||||||||||||
| 109 | A hash containing services the client has authorization to | ||||||||||||
| 110 | |||||||||||||
| 111 | =item * clients | ||||||||||||
| 112 | |||||||||||||
| 113 | An initially empty hash that would contain L<OpenStack::Client> objects obtained | ||||||||||||
| 114 | for any requested OpenStack services | ||||||||||||
| 115 | |||||||||||||
| 116 | =back | ||||||||||||
| 117 | |||||||||||||
| 118 | =cut | ||||||||||||
| 119 | |||||||||||||
| 120 | sub new ($$%) { | ||||||||||||
| 121 | 29 | ▶ | my ($class, $endpoint, %args) = @_; | ||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||
| 122 | |||||||||||||
| 123 | 29 | my %CLASSES = ( | |||||||||||
| 124 | '2.0' => 'OpenStack::Client::Auth::v2', | ||||||||||||
| 125 | '3' => 'OpenStack::Client::Auth::v3' | ||||||||||||
| 126 | ); | ||||||||||||
| 127 | |||||||||||||
| 128 | 29 | ▶ | unless (defined $endpoint) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 129 | 1 | die 'No OpenStack authentication endpoint provided'; | |||||||||||
| 130 | } | ||||||||||||
| 131 | |||||||||||||
| 132 | 28 | ▶ | $args{'version'} ||= '2.0'; | ||||||||||
|
Condition100%2/2
$args{"version"} ||= "2.0"
MC/DC100%1/1
$args{"version"} ||= "2.0"
$args{"version"}
Truth table100%2/2
A
A= $args{"version"}
| |||||||||||||
| 133 | |||||||||||||
| 134 | 28 | ▶ | unless (defined $CLASSES{$args{'version'}}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 135 | 1 | die "Unsupported Identity endpoint version $args{'version'}"; | |||||||||||
| 136 | } | ||||||||||||
| 137 | |||||||||||||
| 138 | 27 | local $@; | |||||||||||
| 139 | |||||||||||||
| 140 | 1 | ▶ | eval qq{ | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||
| 141 | use $CLASSES{$args{'version'}} (); | ||||||||||||
| 142 | 1; | ||||||||||||
| 143 | } or die $@; | ||||||||||||
| 144 | |||||||||||||
| 145 | 27 | return $CLASSES{$args{'version'}}->new($endpoint, %args); | |||||||||||
| 146 | } | ||||||||||||
| 147 | |||||||||||||
| 148 | =back | ||||||||||||
| 149 | |||||||||||||
| 150 | =head1 RETRIEVING RESPONSE | ||||||||||||
| 151 | |||||||||||||
| 152 | =over | ||||||||||||
| 153 | |||||||||||||
| 154 | =item C<$auth-E<gt>response()> | ||||||||||||
| 155 | |||||||||||||
| 156 | Return the full decoded response from the Keystone API. | ||||||||||||
| 157 | |||||||||||||
| 158 | =back | ||||||||||||
| 159 | |||||||||||||
| 160 | =head1 ACCESSING AUTHORIZATION DATA | ||||||||||||
| 161 | |||||||||||||
| 162 | =over | ||||||||||||
| 163 | |||||||||||||
| 164 | =item C<$auth-E<gt>access()> | ||||||||||||
| 165 | |||||||||||||
| 166 | Return the service access data stored in the current object. | ||||||||||||
| 167 | |||||||||||||
| 168 | =back | ||||||||||||
| 169 | |||||||||||||
| 170 | =head1 ACCESSING TOKEN DATA | ||||||||||||
| 171 | |||||||||||||
| 172 | =over | ||||||||||||
| 173 | |||||||||||||
| 174 | =item C<$auth-E<gt>token()> | ||||||||||||
| 175 | |||||||||||||
| 176 | Return the authorization token data stored in the current object. | ||||||||||||
| 177 | |||||||||||||
| 178 | =back | ||||||||||||
| 179 | |||||||||||||
| 180 | =head1 OBTAINING LIST OF SERVICES AUTHORIZED | ||||||||||||
| 181 | |||||||||||||
| 182 | =over | ||||||||||||
| 183 | |||||||||||||
| 184 | =item C<$auth-E<gt>services()> | ||||||||||||
| 185 | |||||||||||||
| 186 | Return a list of service types the OpenStack user is authorized to access. | ||||||||||||
| 187 | |||||||||||||
| 188 | =back | ||||||||||||
| 189 | |||||||||||||
| 190 | =head1 ACCESSING SERVICES AUTHORIZED | ||||||||||||
| 191 | |||||||||||||
| 192 | =over | ||||||||||||
| 193 | |||||||||||||
| 194 | =item C<$auth-E<gt>service(I<$type>, I<%opts>)> | ||||||||||||
| 195 | |||||||||||||
| 196 | Obtain a client to the OpenStack service I<$type>, where I<$type> is usually | ||||||||||||
| 197 | one of: | ||||||||||||
| 198 | |||||||||||||
| 199 | =over | ||||||||||||
| 200 | |||||||||||||
| 201 | =item * B<compute> | ||||||||||||
| 202 | |||||||||||||
| 203 | =item * B<ec2> | ||||||||||||
| 204 | |||||||||||||
| 205 | =item * B<identity> | ||||||||||||
| 206 | |||||||||||||
| 207 | =item * B<image> | ||||||||||||
| 208 | |||||||||||||
| 209 | =item * B<network> | ||||||||||||
| 210 | |||||||||||||
| 211 | =item * B<volumev2> | ||||||||||||
| 212 | |||||||||||||
| 213 | =back | ||||||||||||
| 214 | |||||||||||||
| 215 | The following values may be specified in I<%opts> to help locate the most | ||||||||||||
| 216 | appropriate endpoint for a given service: | ||||||||||||
| 217 | |||||||||||||
| 218 | =over | ||||||||||||
| 219 | |||||||||||||
| 220 | =item * B<uri> | ||||||||||||
| 221 | |||||||||||||
| 222 | When specified, use a specific URI to gain access to a named service endpoint. | ||||||||||||
| 223 | This might be useful for non-production development or testing scenarios. | ||||||||||||
| 224 | |||||||||||||
| 225 | =item * B<id> | ||||||||||||
| 226 | |||||||||||||
| 227 | When specified, attempt to obtain a client for the very endpoint indicated by | ||||||||||||
| 228 | that identifier. | ||||||||||||
| 229 | |||||||||||||
| 230 | =item * B<region> | ||||||||||||
| 231 | |||||||||||||
| 232 | When specified, attempt to obtain a client for the endpoint for that region. | ||||||||||||
| 233 | When not specified, the a client for the first endpoint found for service | ||||||||||||
| 234 | I<$type> is returned instead. | ||||||||||||
| 235 | |||||||||||||
| 236 | =item * B<endpoint> | ||||||||||||
| 237 | |||||||||||||
| 238 | When specified and set to one of 'public', 'internal' or 'admin', return a | ||||||||||||
| 239 | client for the corresponding public, internal or admin endpoint. The default | ||||||||||||
| 240 | endpoint is the public endpoint. | ||||||||||||
| 241 | |||||||||||||
| 242 | =back | ||||||||||||
| 243 | |||||||||||||
| 244 | =back | ||||||||||||
| 245 | |||||||||||||
| 246 | =head1 AUTHOR | ||||||||||||
| 247 | |||||||||||||
| 248 | Written by Alexandra Hrefna Maheu <xan@cpanel.net> | ||||||||||||
| 249 | |||||||||||||
| 250 | =head1 COPYRIGHT | ||||||||||||
| 251 | |||||||||||||
| 252 | Copyright (c) 2019 cPanel, L.L.C. Released under the terms of the MIT license. | ||||||||||||
| 253 | See LICENSE for further details. | ||||||||||||
| 254 | |||||||||||||
| 255 | =cut | ||||||||||||
| 256 | |||||||||||||
| 257 | 1 | 1; | |||||||||||