Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update AWX collection to use basic authentication #15554

Merged

Conversation

ldjebran
Copy link
Contributor

@ldjebran ldjebran commented Sep 27, 2024

SUMMARY

Update AWX collection to use basic authentication when collection_type is awx,
else try to create a token from gateway and controller when failed todo so use basic authentication instead.

ISSUE TYPE
  • New or Enhanced Feature
COMPONENT NAME
  • Collection
AWX VERSION
awx: 24.6.2.dev122+g4ce2947dcf
ADDITIONAL INFORMATION

Tested when connected to awx service with _COLLETION_TYPE ="awx"

- name: Test collection with auth
  hosts: localhost
  connection: local
  become: false
  gather_facts: false
  vars:
    controller_host: https://localhost:8043
    source_job_template_name: "Demo Job Template"
    job_template_name: "Demo Job Template 1"
    organization: Default
    controller_username: admin
    controller_password: <hidden-awx-admin-password>
  tasks:
    - name: Create a copy of Demo Job Template
      awx.awx.job_template:
        controller_host: "{{ controller_host }}"
        validate_certs: false
        controller_username: "{{ controller_username }}"
        controller_password: "{{ controller_password }}"
        organization: "{{ organization }}"
        copy_from: "{{ source_job_template_name }}"
        name: "{{ job_template_name }}"
        state: exists
      register: demo_template_copy

    - name: Delete the copy of Demo Job Template
      awx.awx.job_template:
        controller_host: "{{ controller_host }}"
        validate_certs: false
        controller_username: "{{ controller_username }}"
        controller_password: "{{ controller_password }}"
        organization: "{{ organization }}"
        name: "{{ job_template_name }}"
        state: absent

    - name: Try to copy a template with non existent user
      awx.awx.job_template:
        controller_host: "{{ controller_host }}"
        validate_certs: false
        controller_username: does_not_exit
        controller_password: any-password
        organization: "{{ organization }}"
        copy_from: "{{ source_job_template_name }}"
        name: "{{ job_template_name }}"
        state: exists
      register: response
      failed_when: '"Failed to get user info: HTTP Error 401: Unauthorized" not in response.msg'
$ ansible-playbook test_collection_awx.yml
does not match 'all'

PLAY [Test collection with auth] ***********************************************************************

TASK [Create a copy of Demo Job Template] **************************************************************
changed: [localhost]

TASK [Delete the copy of Demo Job Template] ************************************************************
changed: [localhost]

TASK [Try to copy a template with non existent user] ****************************************************
ok: [localhost]

PLAY RECAP *********************************************************************************************
localhost                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Tested when connected to proxy with _COLLETION_TYPE ="tower"

- name: Test controller with proxy gateway
  hosts: localhost
  connection: local
  become: false
  gather_facts: false
  vars:
    controller_host: https://localhost
    source_job_template_name: "Demo Job Template"
    job_template_name: "Demo Job Template 1"
    organization: Default
    controller_username: admin
    controller_password: <hidden-gateway-admin-password>
# same content as first one     

Result

$ ansible-playbook test_collection_proxy.yml 
PLAY [Test controller with proxy gateway] **************************************************************

TASK [Create a copy of Demo Job Template] **************************************************************
changed: [localhost]

TASK [Delete the copy of Demo Job Template] ************************************************************
changed: [localhost]

TASK [Try to copy a template with non existent user] ***************************************************
[WARNING]: url: https://localhost/api/gateway/v1/tokens/ - Failed to get token: HTTP Error 401:
Unauthorized
[WARNING]: url: https://localhost/api/controller/v2/tokens/ - Failed to get token: HTTP Error 401:
Unauthorized
ok: [localhost]

PLAY RECAP *********************************************************************************************
localhost                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

@github-actions github-actions bot added component:awx_collection issues related to the collection for controlling AWX community labels Sep 27, 2024
@ldjebran ldjebran force-pushed the awx-update-awx-collection-basic-auth branch from 4ce2947 to 8e8b94f Compare September 27, 2024 09:59
@ldjebran ldjebran force-pushed the awx-update-awx-collection-basic-auth branch 8 times, most recently from b697078 to dd58af3 Compare September 30, 2024 11:56
@ldjebran ldjebran force-pushed the awx-update-awx-collection-basic-auth branch 6 times, most recently from f390da9 to 4bd34d9 Compare October 3, 2024 07:47
Update AWX collection to use basic authentication when oauth token not provided,
and when username and password provided.
@ldjebran ldjebran force-pushed the awx-update-awx-collection-basic-auth branch from 4bd34d9 to 263d82f Compare October 3, 2024 07:50
Copy link

sonarcloud bot commented Oct 3, 2024

@TheRealHaoLiu TheRealHaoLiu enabled auto-merge (squash) October 8, 2024 14:36
@TheRealHaoLiu TheRealHaoLiu merged commit 579c2b7 into ansible:devel Oct 8, 2024
21 of 25 checks passed


@pytest.mark.parametrize(
"collection_type, env_prefix, controller_host, app_key, endpoint, expected",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pro tip: this can be an interable of strings which typically reads better.

from __future__ import absolute_import, division, print_function

import os
from unittest import mock
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In pytest, it's customary to use the built-in monkeypatch fixture or mocker provided by the pytest-mock plugin.

@@ -0,0 +1,55 @@
from __future__ import absolute_import, division, print_function
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this future import is only useful under python 2

controller_api = controller_api_class(argument_spec={}, direct_params=dict(controller_host=controller_host))
controller_api._COLLECTION_TYPE = collection_type
if env_prefix:
with mock.patch.dict(os.environ, {"CONTROLLER_OPTIONAL_API_URLPATTERN_PREFIX": env_prefix}):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For conditional CM, there's a from contextlib import nullcontext which can make this more elegant.
Additionally, the builtin fixture can give you a monkeypatch.setenv() w/o needing to import anything or integrate third parties.

import pytest


@pytest.mark.parametrize(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a matrix of repeated things. It is possible to produce it using several stacked parametrize decorators.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community component:awx_collection issues related to the collection for controlling AWX
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants