Last active
September 14, 2018 18:24
-
-
Save robbmanes/ffb80b6b727ef81bd28069a784779ddd to your computer and use it in GitHub Desktop.
Ansible Tower API authentication test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'logger' | |
require 'rest-client' | |
require 'ansible_tower_client' | |
require 'json' | |
module AnsibleTestSuite | |
class AnsibleTest | |
TOWER_URL = "https://my-tower-address.com" | |
TOWER_USER = "admin" | |
TOWER_PASSWORD = "tower_password" | |
def initialize | |
@logger = Logger.new("/root/ansible_auth_test.log") | |
end | |
def test_restclient_authtoken | |
@logger.info("Testing Tower connection using regular OAuth2...") | |
token = '' | |
# Obtain an auth token | |
RestClient::Request.new({ | |
method: "post", | |
url: TOWER_URL + "/api/v2/authtoken/", | |
payload: JSON.generate({ username: TOWER_USER, | |
password: TOWER_PASSWORD | |
}), | |
headers: { content_type: :json }, | |
verify_ssl: false | |
}).execute do |response, request, result| | |
token = JSON.parse(response.body)['token'] | |
case response.code | |
when 200 | |
@logger.info("Successfully obtained authtoken [#{token}].") | |
else | |
@logger.error("Failed to obtain token: #{response}") | |
return | |
end | |
end | |
response = RestClient::Request.new({ | |
method: "get", | |
url: TOWER_URL + "/api/v2/me/", | |
headers: { :Authorization => "Bearer #{token}" }, | |
verify_ssl: false | |
}).execute | |
response = RestClient::Request.new({ | |
method: "get", | |
url: TOWER_URL + "/api/v2/inventories/", | |
headers: { authorization: "Bearer #{token}" }, | |
verify_ssl: false | |
}).execute | |
response = RestClient::Request.new({ | |
method: "get", | |
url: TOWER_URL + "/api/v2/labels/", | |
headers: { authorization: "Bearer #{token}" }, | |
verify_ssl: false | |
}).execute | |
end | |
def test_restclient_password | |
@logger.info("Testing Tower connection using password authentication...") | |
# Hit the endpoints without worrying about auth, just user user/pass in requests | |
response = RestClient::Request.new({ | |
method: "get", | |
url: TOWER_URL + "/api/v2/me/", | |
user: TOWER_USER, | |
password: TOWER_PASSWORD, | |
verify_ssl: false | |
}).execute | |
@logger.info("Attempted endpoint /api/v2/me/, result: #{response}") | |
response = RestClient::Request.new({ | |
method: "get", | |
url: TOWER_URL + "/api/v2/inventories/", | |
user: TOWER_USER, | |
password: TOWER_PASSWORD, | |
verify_ssl: false | |
}).execute | |
response = RestClient::Request.new({ | |
method: "get", | |
url: TOWER_URL + "/api/v2/labels/", | |
user: TOWER_USER, | |
password: TOWER_PASSWORD, | |
verify_ssl: false | |
}).execute | |
end | |
def test_ansiblegem | |
@logger.info("Testing Tower connection using the Ansible Rubygem...") | |
ansible = AnsibleTowerClient::Connection.new(username: TOWER_USER, | |
password: TOWER_PASSWORD, | |
base_url: TOWER_URL, | |
verify_ssl: false, | |
logger: @logger) | |
end | |
end | |
end | |
ansible_test = AnsibleTestSuite::AnsibleTest.new | |
ansible_test.test_restclient_authtoken | |
ansible_test.test_restclient_password | |
ansible_test.test_ansiblegem |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment