-
Notifications
You must be signed in to change notification settings - Fork 7
/
redhat_jws_download.py
executable file
·65 lines (47 loc) · 1.73 KB
/
redhat_jws_download.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/python3
# Download jws zip from the Red Hat customer portal
# using username/password from a docker secret file.
#
# The most easy is to use podman login registry.redhat.io to create
# the username/password in $HOME/.docker/config.json and use that
# file.
#
# of course to have create the username/password in red hat portal
##############################################################################
import requests
import lxml.html
import ujson
import base64
import sys
def get_jws_file(username,password,url,dest):
# Setup Auth Struct
auth = {'j_username': username, 'j_password': password}
session = requests.Session()
# Get initial request
r = session.get(url)
# Parse initial response
root = lxml.html.fromstring(r.text)
post_url = root.xpath('//form[@method="post"]')[0].action
# Final Post to download file
data = {'username': username, 'password': password}
r = session.post(post_url, data=data)
# Download file
with open(dest, "wb") as code:
code.write(r.content)
# Close session
session.close()
# read the file name (from a mounted secret in kubernetes)
if len(sys.argv) != 4:
sys.exit("Need a file name like: /home/jfclere/.docker/config.json, an url like https://access.redhat.com/jbossnetwork/restricted/softwareDownload.html?softwareId=37193 and a destination file")
# Opening JSON file
f = open(sys.argv[1])
# returns JSON object as
# a dictionary
data = ujson.load(f)['auths']['registry.redhat.io']['auth']
auth = base64.b64decode(data)
userpass = auth.split(b':')
username = userpass[0].decode('ascii')
password = userpass[1].decode('ascii')
url = sys.argv[2]
dest = sys.argv[3]
get_jws_file(username,password,url,dest)