Skip to content

Commit

Permalink
Add helper methods to add user agent components
Browse files Browse the repository at this point in the history
  • Loading branch information
kdaily committed Apr 22, 2024
1 parent da00055 commit d2d627d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 30 deletions.
18 changes: 3 additions & 15 deletions awscli/botocore/useragent.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,6 @@ def to_string(self):
return self._value


# This is not a public interface and is subject to abrupt breaking changes.
# Any usage is not advised or supported in external code bases.
try:
from botocore.customizations.useragent import modify_components
except ImportError:
# Default implementation that returns unmodified User-Agent components.
def modify_components(components):
return components


class UserAgentString:
"""
Generator for AWS SDK User-Agent header strings.
Expand Down Expand Up @@ -282,8 +272,6 @@ def to_string(self):
*self._build_extra(),
]

components = modify_components(components)

return ' '.join([comp.to_string() for comp in components])

def _build_sdk_metadata(self):
Expand All @@ -310,9 +298,9 @@ def _build_sdk_metadata(self):
self._session_user_agent_name,
self._session_user_agent_version,
),
# UserAgentComponent(
# 'md', _USERAGENT_SDK_NAME, botocore_version
# ),
UserAgentComponent(
'md', _USERAGENT_SDK_NAME, botocore_version
),
]
)
else:
Expand Down
2 changes: 2 additions & 0 deletions awscli/botocore/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io
import logging
import os
import platform
import random
import re
import socket
Expand All @@ -34,6 +35,7 @@
import botocore.awsrequest
import botocore.httpsession
import dateutil.parser
import distro
from botocore.compat import (
MD5_AVAILABLE,
get_md5,
Expand Down
49 changes: 37 additions & 12 deletions awscli/clidriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from botocore.configprovider import ScopedConfigProvider
from botocore.configprovider import ConstantProvider
from botocore.configprovider import ChainProvider
from botocore.useragent import UserAgentComponent, RawStringUserAgentComponent

from awscli import __version__
from awscli.compat import (
Expand Down Expand Up @@ -55,6 +56,7 @@
set_stream_logger, remove_stream_logger, enable_crt_logging,
disable_crt_logging,
)
from awscli.utils import add_component_to_user_agent_extra
from awscli.utils import emit_top_level_args_parsed_event
from awscli.utils import OutputStreamFactory
from awscli.utils import IMDSRegionProvider
Expand Down Expand Up @@ -134,17 +136,26 @@ def _get_linux_distribution():
return linux_distribution


def _add_distribution_source_to_user_agent(session):
add_component_to_user_agent_extra(
session,
UserAgentComponent('md', 'installer', _get_distribution_source())
)


def _add_linux_distribution_to_user_agent(session):
if linux_distribution := _get_distribution():
add_component_to_user_agent_extra(
session,
UserAgentComponent('md', 'distrib', linux_distribution)
)


def _set_user_agent_for_session(session):
session.user_agent_name = 'aws-cli'
session.user_agent_version = __version__
# user_agent_extra on linux will look like "rpm/x86_64.Ubuntu.18"
# on mac and windows like "sources/x86_64"
session.user_agent_extra = 'lib/awscli md/installer#%s' % (
_get_distribution_source(),
)
linux_distribution = _get_distribution()
if linux_distribution:
session.user_agent_extra += " md/distrib#%s" % linux_distribution
_add_distribution_source_to_user_agent(session)
_add_linux_distribution_to_user_agent(session)


def no_pager_handler(session, parsed_args, **kwargs):
Expand Down Expand Up @@ -174,8 +185,14 @@ def main(self, args):
HISTORY_RECORDER.record('CLI_RC', rc, 'CLI')
return rc

def _add_autoprompt_to_user_agent(self, driver, prompt_mode):
add_component_to_user_agent_extra(
driver.session,
UserAgentComponent("md", "prompt", prompt_mode)
)

def _run_driver(self, driver, args, prompt_mode):
driver.session.user_agent_extra += " md/prompt#%s" % prompt_mode
self._add_autoprompt_to_user_agent(driver, prompt_mode)
return driver.main(args)

def _do_main(self, args):
Expand Down Expand Up @@ -882,10 +899,18 @@ def _create_operation_parser(self, arg_table, subcommand_table):

def _add_customization_to_user_agent(self):
if ' md/command#' in self._session.user_agent_extra:
self._session.user_agent_extra += '.%s' % self.lineage_names[-1]
add_component_to_user_agent_extra(
self._session,
RawStringUserAgentComponent(f".{self.lineage_names[-1]}")
)
else:
self._session.user_agent_extra += ' md/command#%s' % '.'.join(
self.lineage_names
add_component_to_user_agent_extra(
self._session,
UserAgentComponent(
"md",
"command",
'.'.join(self.lineage_names)
)
)


Expand Down
16 changes: 13 additions & 3 deletions awscli/customizations/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from botocore import model
from botocore.compat import OrderedDict
from botocore.validate import validate_parameters
from botocore.useragent import UserAgentComponent, RawStringUserAgentComponent

import awscli
from awscli.argparser import ArgTableArgParser, SubCommandArgParser
Expand All @@ -15,6 +16,7 @@
from awscli.bcdoc import docevents
from awscli.help import HelpCommand
from awscli.schema import SchemaTransformer
from awscli.utils import add_component_to_user_agent_extra
from awscli.customizations.exceptions import ParamValidationError

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -325,10 +327,18 @@ def _raise_usage_error(self):

def _add_customization_to_user_agent(self):
if ' md/command#' in self._session.user_agent_extra:
self._session.user_agent_extra += '.%s' % self.lineage_names[-1]
add_component_to_user_agent_extra(
self._session,
RawStringUserAgentComponent(f".{self.lineage_names[-1]}")
)
else:
self._session.user_agent_extra += ' md/command#%s' % '.'.join(
self.lineage_names
add_component_to_user_agent_extra(
self._session,
UserAgentComponent(
"md",
"command",
'.'.join(self.lineage_names)
)
)


Expand Down
6 changes: 6 additions & 0 deletions awscli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,9 @@ def __init__(self):

def visit_shape(self, shape):
self.visited.append(shape)


def add_component_to_user_agent_extra(session, component):
if session.user_agent_extra and not session.user_agent_extra.endswith(" "):
session.user_agent_extra += " "
session.user_agent_extra += f"{component.to_string()}"

0 comments on commit d2d627d

Please sign in to comment.