Skip to content

Instantly share code, notes, and snippets.

@alikins
Last active March 19, 2020 11:29
Show Gist options
  • Save alikins/85359a201a3852050882e2b18a8976dd to your computer and use it in GitHub Desktop.
Save alikins/85359a201a3852050882e2b18a8976dd to your computer and use it in GitHub Desktop.
how to use mitmweb proxy to add auth headers for galaxy-api
# use with:
# mitmweb --scripts add_x_rh_id_header.py
#
# Full example:
# mitmweb -v --scripts mitmproxy_scripts/add_x_rh_id_header.py --listen-port 8088 --web-port 8089 -k --set ah_username=alikins
#
import base64
from mitmproxy import ctx
def user_x_rh_identity(username, account_number=None):
account_number = account_number or "12345"
title_username = username.title()
token_json = """{
"entitlements":
{"insights":
{"is_entitled": true}
},
"identity":
{"account_number": "%(account_number)s",
"user":
{"username": "%(username)s",
"email": "%(username)[email protected]",
"first_name": "%(title_username)s",
"last_name": "%(title_username)sington"
},
"internal": {"org_id": "54321"}
}
}""" % {'username': username, 'title_username': title_username,
'account_number': account_number}
token_b64 = base64.b64encode(token_json.encode())
return token_b64
class AddHeader:
def __init__(self):
pass
# self.x_rh_id = """ewogICAgImVudGl0bGVtZW50cyI6CiAgICAgICAgeyJpbnNpZ2h0cyI6CiAgICAgICAgICAgIHsiaXNfZW50aXRsZWQiOiB0cnVlfQogICAgICAgIH0sCiAgICAiaWRlbnRpdHkiOgogICAgICAgIHsiYWNjb3VudF9udW1iZXIiOiAiMTIzNDUiLAogICAgICAgICAidXNlciI6CiAgICAgICAgICAgIHsidXNlcm5hbWUiOiAiYWxpa2lucyIsCiAgICAgICAgICAgICAiZW1haWwiOiAiYWxpa2lucytib2JjaXNjb0ByZWRoYXQuY29tIiwKICAgICAgICAgICAgICJmaXJzdF9uYW1lIjogIkFkcmlhbiIsCiAgICAgICAgICAgICAibGFzdF9uYW1lIjogIkxpa2lucyIKICAgICAgICAgICAgIH0sCiAgICAgICAgICJpbnRlcm5hbCI6IHsib3JnX2lkIjogIjU0MzIxIn0KICAgICAgICAgfQp9Cg=="""
def load(self, loader):
loader.add_option(name='ah_username',
typespec=str,
default='ansible-insights',
help='create an x-rh-identity header for the username')
loader.add_option(name='ah_account_number',
typespec=str,
default='12345',
help='create an x-rh-identity header for the account number')
def request(self, flow):
if ctx.options.ah_username:
# flow.request.headers["x-rh-identity"] = str(self.x_rh_id)
flow.request.headers["x-rh-identity"] = user_x_rh_identity(ctx.options.ah_username,
account_number=str(ctx.options.ah_account_number))
ctx.log.info("Added x-rh-id header to request for ah_username: %s ah_account_number: %s" %
(str(ctx.options.ah_username),
str(ctx.options.ah_account_number)),
)
addons = [
AddHeader()
]
@romanblanco
Copy link

romanblanco commented Nov 21, 2019

pip install httpie --user
pip install mitmproxy --user

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment