-
Notifications
You must be signed in to change notification settings - Fork 336
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
Add encryption to azure_rm_galleryimageversion #1311
Merged
xuzhang3
merged 3 commits into
ansible-collections:dev
from
ephracis:1290-add-encryption-to-gallery-image-version
Nov 9, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
92 changes: 92 additions & 0 deletions
92
.../integration/targets/azure_rm_gallery/lookup_plugins/azure_service_principal_attribute.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# (c) 2018 Yunge Zhu, <yungez@microsoft.com> | ||
# (c) 2017 Ansible Project | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
from __future__ import (absolute_import, division, print_function) | ||
__metaclass__ = type | ||
|
||
DOCUMENTATION = """ | ||
lookup: azure_service_principal_attribute | ||
|
||
requirements: | ||
- azure-graphrbac | ||
|
||
author: | ||
- Yunge Zhu <yungez@microsoft.com> | ||
|
||
version_added: "2.7" | ||
|
||
short_description: Look up Azure service principal attributes. | ||
|
||
description: | ||
- Describes object id of your Azure service principal account. | ||
options: | ||
azure_client_id: | ||
description: azure service principal client id. | ||
azure_secret: | ||
description: azure service principal secret | ||
azure_tenant: | ||
description: azure tenant | ||
azure_cloud_environment: | ||
description: azure cloud environment | ||
""" | ||
|
||
EXAMPLES = """ | ||
set_fact: | ||
object_id: "{{ lookup('azure_service_principal_attribute', | ||
azure_client_id=azure_client_id, | ||
azure_secret=azure_secret, | ||
azure_tenant=azure_secret) }}" | ||
""" | ||
|
||
RETURN = """ | ||
_raw: | ||
description: | ||
Returns object id of service principal. | ||
""" | ||
|
||
from ansible.errors import AnsibleError | ||
from ansible.plugins.lookup import LookupBase | ||
from ansible.module_utils._text import to_native | ||
|
||
try: | ||
from azure.common.credentials import ServicePrincipalCredentials | ||
from azure.graphrbac import GraphRbacManagementClient | ||
from azure.cli.core import cloud as azure_cloud | ||
except ImportError: | ||
raise AnsibleError( | ||
"The lookup azure_service_principal_attribute requires azure.graphrbac, msrest") | ||
|
||
|
||
class LookupModule(LookupBase): | ||
def run(self, terms, variables, **kwargs): | ||
|
||
self.set_options(direct=kwargs) | ||
|
||
credentials = {} | ||
credentials['azure_client_id'] = self.get_option('azure_client_id', None) | ||
credentials['azure_secret'] = self.get_option('azure_secret', None) | ||
credentials['azure_tenant'] = self.get_option('azure_tenant', 'common') | ||
|
||
if credentials['azure_client_id'] is None or credentials['azure_secret'] is None: | ||
raise AnsibleError("Must specify azure_client_id and azure_secret") | ||
|
||
_cloud_environment = azure_cloud.AZURE_PUBLIC_CLOUD | ||
if self.get_option('azure_cloud_environment', None) is not None: | ||
cloud_environment = azure_cloud.get_cloud_from_metadata_endpoint(credentials['azure_cloud_environment']) | ||
|
||
try: | ||
azure_credentials = ServicePrincipalCredentials(client_id=credentials['azure_client_id'], | ||
secret=credentials['azure_secret'], | ||
tenant=credentials['azure_tenant'], | ||
resource=_cloud_environment.endpoints.active_directory_graph_resource_id) | ||
|
||
client = GraphRbacManagementClient(azure_credentials, credentials['azure_tenant'], | ||
base_url=_cloud_environment.endpoints.active_directory_graph_resource_id) | ||
|
||
response = list(client.service_principals.list(filter="appId eq '{0}'".format(credentials['azure_client_id']))) | ||
sp = response[0] | ||
|
||
return sp.object_id.split(',') | ||
except Exception as ex: | ||
raise AnsibleError("Failed to get service principal object id: %s" % to_native(ex)) | ||
return False |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ephracis you are the author.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xuzhang3 This file was copied from another location and no changes are required to the author. Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's true. It would actually be better to put the file somewhere where it could be shared by both integration tests but this was my first attempt at it.
If you have any suggestions on where to put the file and then share it, let me know and I can fix it as part of this PR.
I'm actually wondering why this is not part of the collection itself, and only part of the integration tests. Seems like a good plugin to have. But perhaps there's a reason you don't want it shipped.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ephracis Yes, we are considering it as a plugin that can be applied to the entire repo. Thanks!