Skip to content

Commit

Permalink
Merge pull request #64 from Thomas-Franklin/creds-consistency
Browse files Browse the repository at this point in the history
(FM-7400) consistency of credentials files without device modules
  • Loading branch information
DavidS authored Sep 24, 2018
2 parents 98cf7de + a6f9f94 commit dcc97b8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,20 @@ There are two valid types of credential files:

* (a) A file containing the host, username and password in plain text, for example:
```
host: 10.0.10.20
user: admin
address: 10.0.10.20
username: admin
password: admin
```
* (b) A file containing the host and an API key obtained from the device, for example:
* (b) A file containing the address and an API key obtained from the device, for example:
```
host: 10.0.10.20
address: 10.0.10.20
apikey: LUFRPT10cHhRNXMyR2wrYW1MSzg5cldhNElodmVkL1U9OEV1cGY5ZjJyc2xGL1Z4Qk9TNFM2dz09
```

__Note:__ v0.1.0 requires `host` instead of `address`

__Note:__ v0.1.0 requires `user` instead of `username`

To obtain an API key for the device, it is possible to use the `panos::apikey` task. The required creditials file should be in the format of (a) above. After which you can discard it. Before running this task, install the module on your machine, along with [Puppet Bolt](https://puppet.com/docs/bolt/0.x/bolt_installing.html). When complete, execute the following command:

```
Expand Down
10 changes: 6 additions & 4 deletions lib/puppet/util/network_device/panos/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ def facts
end

def config
raise Puppet::ResourceError, 'Could not find host in the configuration' unless super.key?('host')
raise Puppet::ResourceError, 'Could not find host or address in the configuration' unless super.key?('host') || super.key?('address')
raise Puppet::ResourceError, 'The port attribute in the configuration is not an integer' if super.key?('port') && super['port'] !~ %r{\A[0-9]+\Z}
raise Puppet::ResourceError, 'Could not find user/password or apikey in the configuration' unless (super.key?('user') && super.key?('password')) || super.key?('apikey')
raise Puppet::ResourceError, 'Could not find user/password or apikey in the configuration' unless ((super.key?('user') || super.key?('username')) && super.key?('password')) || super.key?('apikey') # rubocop:disable Metrics/LineLength
raise Puppet::ResourceError, 'User and username are mutually exclusive' if super.key?('user') && super.key?('username')
raise Puppet::ResourceError, 'Host and address are mutually exclusive' if super.key?('host') && super.key?('address')
super
end

Expand Down Expand Up @@ -112,9 +114,9 @@ def api
# @api private
class API
def initialize(credentials)
@host = credentials['host']
@host = credentials['host'] || credentials['address']
@port = credentials.key?('port') ? credentials['port'].to_i : 443
@user = credentials['user']
@user = credentials['user'] || credentials['username']
@password = credentials['password']
@apikey = credentials['apikey']
end
Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper_acceptance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def debug_output?

File.open('spec/fixtures/acceptance-credentials.conf', 'w') do |file|
file.puts <<CREDENTIALS
host: #{@hostname}
user: #{ENV['PANOS_TEST_USER'] || 'admin'}
address: #{@hostname}
username: #{ENV['PANOS_TEST_USER'] || 'admin'}
password: #{ENV['PANOS_TEST_PASSWORD'] || 'admin'}
CREDENTIALS
end
Expand Down
25 changes: 23 additions & 2 deletions spec/unit/puppet/util/network_device/panos/device_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
context 'when host is not provided' do
let(:device_config) { { 'user' => 'admin', 'password' => 'password' } }

it { expect { device.config }.to raise_error Puppet::ResourceError, 'Could not find host in the configuration' }
it { expect { device.config }.to raise_error Puppet::ResourceError, 'Could not find host or address in the configuration' }
end
context 'when port is provided but not valid' do
let(:device_config) { { 'host' => 'www.example.com', 'port' => 'foo', 'user' => 'admin', 'password' => 'password' } }
Expand All @@ -58,6 +58,7 @@
[
{ 'host' => 'www.example.com', 'user' => 'admin' },
{ 'host' => 'www.example.com', 'password' => 'password' },
{ 'host' => 'www.example.com', 'username' => 'admin' },
{ 'host' => 'www.example.com' },
].each do |config|
let(:device_config) { config }
Expand All @@ -70,11 +71,31 @@

it { expect { device.config }.not_to raise_error Puppet::ResourceError }
end
context 'when username and password is provided' do
context 'when `user` and password is provided' do
let(:device_config) { { 'host' => 'www.example.com', 'user' => 'foo', 'password' => 'password' } }

it { expect { device.config }.not_to raise_error Puppet::ResourceError }
end
context 'when `username` and password is provided' do
let(:device_config) { { 'host' => 'www.example.com', 'username' => 'foo', 'password' => 'password' } }

it { expect { device.config }.not_to raise_error Puppet::ResourceError }
end
context 'when `host` and `address` and password is provided' do
let(:device_config) { { 'host' => 'www.example.com', 'address' => 'www.example.com', 'username' => 'foo', 'password' => 'password' } }

it { expect { device.config }.to raise_error Puppet::ResourceError, 'Host and address are mutually exclusive' }
end
context 'when `address` is provided' do
let(:device_config) { { 'address' => 'www.example.com', 'username' => 'foo', 'password' => 'password' } }

it { expect { device.config }.not_to raise_error }
end
context 'when `user` and `username` and password is provided' do
let(:device_config) { { 'host' => 'www.example.com', 'user' => 'foo', 'username' => 'foo', 'password' => 'password' } }

it { expect { device.config }.to raise_error Puppet::ResourceError, 'User and username are mutually exclusive' }
end
end

describe 'helper functions' do
Expand Down

0 comments on commit dcc97b8

Please sign in to comment.