Skip to content

Commit a3a6976

Browse files
authored
Merge pull request #22 from linuxserver/py3
Switch to Python 3
2 parents 48d57f8 + 12633b3 commit a3a6976

File tree

11 files changed

+67
-36
lines changed

11 files changed

+67
-36
lines changed

Dockerfile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,18 @@ RUN \
1313
build-base \
1414
libffi-dev \
1515
openldap-dev \
16-
python2-dev && \
16+
python3-dev && \
1717
echo "**** install runtime packages ****" && \
1818
apk add --no-cache \
1919
libffi \
2020
libldap \
21-
py2-pip \
22-
python2 && \
21+
python3 && \
2322
if [ -z ${LDAP_VERSION+x} ]; then \
2423
LDAP_INSTALL="python-ldap"; \
2524
else \
2625
LDAP_INSTALL="python-ldap==${LDAP_VERSION}"; \
2726
fi && \
28-
pip install -U --no-cache-dir \
27+
pip3 install -U --no-cache-dir \
2928
pip && \
3029
pip install -U \
3130
cryptography \

Dockerfile.aarch64

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,18 @@ RUN \
1313
build-base \
1414
libffi-dev \
1515
openldap-dev \
16-
python2-dev && \
16+
python3-dev && \
1717
echo "**** install runtime packages ****" && \
1818
apk add --no-cache \
1919
libffi \
2020
libldap \
21-
py2-pip \
22-
python2 && \
21+
python3 && \
2322
if [ -z ${LDAP_VERSION+x} ]; then \
2423
LDAP_INSTALL="python-ldap"; \
2524
else \
2625
LDAP_INSTALL="python-ldap==${LDAP_VERSION}"; \
2726
fi && \
28-
pip install -U --no-cache-dir \
27+
pip3 install -U --no-cache-dir \
2928
pip && \
3029
pip install -U \
3130
cryptography \

Dockerfile.armhf

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,18 @@ RUN \
1313
build-base \
1414
libffi-dev \
1515
openldap-dev \
16-
python2-dev && \
16+
python3-dev && \
1717
echo "**** install runtime packages ****" && \
1818
apk add --no-cache \
1919
libffi \
2020
libldap \
21-
py2-pip \
22-
python2 && \
21+
python3 && \
2322
if [ -z ${LDAP_VERSION+x} ]; then \
2423
LDAP_INSTALL="python-ldap"; \
2524
else \
2625
LDAP_INSTALL="python-ldap==${LDAP_VERSION}"; \
2726
fi && \
28-
pip install -U --no-cache-dir \
27+
pip3 install -U --no-cache-dir \
2928
pip && \
3029
pip install -U \
3130
cryptography \

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ Once registered you can define the dockerfile to use with `-f Dockerfile.aarch64
186186

187187
## Versions
188188

189+
* **20.02.20:** - Switch to python3.
189190
* **19.12.19:** - Rebasing to alpine 3.11.
190191
* **01.07.19:** - Fall back to base64 encoding when basic http auth is used.
191192
* **28.06.19:** - Rebasing to alpine 3.10.

readme-vars.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ app_setup_block: |
3434
3535
# changelog
3636
changelogs:
37+
- { date: "20.02.20:", desc: "Switch to python3." }
3738
- { date: "19.12.19:", desc: "Rebasing to alpine 3.11." }
3839
- { date: "01.07.19:", desc: "Fall back to base64 encoding when basic http auth is used." }
3940
- { date: "28.06.19:", desc: "Rebasing to alpine 3.10." }

root/app/fernet-key.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#!/bin/sh
2-
''''which python2 >/dev/null && exec python2 "$0" "$@" # '''
32
''''which python >/dev/null && exec python "$0" "$@" # '''
43

54
from cryptography.fernet import Fernet
65

76
key = Fernet.generate_key()
8-
print key
7+
print(key)

root/app/ldap-backend-app.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/bin/sh
2-
''''which python2 >/dev/null && exec python2 "$0" "$@" # '''
32
''''which python >/dev/null && exec python "$0" "$@" # '''
43

54
# Copyright (C) 2014-2015 Nginx, Inc.
@@ -10,29 +9,46 @@
109
# 1) accepts GET requests on /login and responds with a login form
1110
# 2) accepts POST requests on /login, sets a cookie, and responds with redirect
1211

13-
import sys, os, signal, base64, Cookie, cgi, urlparse
14-
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
12+
import sys, os, signal, base64, cgi
13+
if sys.version_info.major == 2:
14+
from urlparse import urlparse
15+
from Cookie import BaseCookie
16+
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
17+
elif sys.version_info.major == 3:
18+
from urllib.parse import urlparse
19+
from http.cookies import BaseCookie
20+
from http.server import HTTPServer, BaseHTTPRequestHandler
21+
1522
from cryptography.fernet import Fernet
1623

1724
Listen = ('0.0.0.0', 9000)
1825

1926
import threading
20-
from SocketServer import ThreadingMixIn
27+
if sys.version_info.major == 2:
28+
from SocketServer import ThreadingMixIn
29+
elif sys.version_info.major == 3:
30+
from socketserver import ThreadingMixIn
31+
32+
33+
def ensure_bytes(data):
34+
return data if sys.version_info.major == 2 else data.encode("utf-8")
35+
36+
2137
class AuthHTTPServer(ThreadingMixIn, HTTPServer):
2238
pass
2339

2440
class AppHandler(BaseHTTPRequestHandler):
2541

2642
def do_GET(self):
2743

28-
url = urlparse.urlparse(self.path)
44+
url = urlparse(self.path)
2945

3046
if url.path.startswith("/login"):
3147
return self.auth_form()
3248

3349
self.send_response(200)
3450
self.end_headers()
35-
self.wfile.write('Hello, world! Requested URL: ' + self.path + '\n')
51+
self.wfile.write(ensure_bytes('Hello, world! Requested URL: ' + self.path + '\n'))
3652

3753

3854
# send login form html
@@ -92,7 +108,7 @@ def auth_form(self, target = None):
92108

93109
self.send_response(200)
94110
self.end_headers()
95-
self.wfile.write(html.replace('TARGET', target))
111+
self.wfile.write(ensure_bytes(html.replace('TARGET', target)))
96112

97113

98114
# processes posted form and sets the cookie with login/password
@@ -118,8 +134,9 @@ def do_POST(self):
118134

119135
self.send_response(302)
120136

121-
cipher_suite = Fernet('REPLACEWITHFERNETKEY')
122-
enc = cipher_suite.encrypt(user + ':' + passwd)
137+
cipher_suite = Fernet(REPLACEWITHFERNETKEY)
138+
enc = cipher_suite.encrypt(ensure_bytes(user + ':' + passwd))
139+
enc = enc.decode()
123140
self.send_header('Set-Cookie', 'nginxauth=' + enc + '; httponly')
124141

125142
self.send_header('Location', target)

root/app/nginx-ldap-auth-daemon.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
#!/bin/sh
22
''''[ -z $LOG ] && export LOG=/dev/stdout # '''
3-
''''which python2 >/dev/null && exec python2 -u "$0" "$@" >> $LOG 2>&1 # '''
43
''''which python >/dev/null && exec python -u "$0" "$@" >> $LOG 2>&1 # '''
54

65
# Copyright (C) 2014-2015 Nginx, Inc.
76
# Copyright (C) 2018 LinuxServer.io
87

9-
import sys, os, signal, base64, ldap, Cookie, argparse
10-
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
8+
import sys, os, signal, base64, ldap, argparse
9+
if sys.version_info.major == 2:
10+
from Cookie import BaseCookie
11+
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
12+
elif sys.version_info.major == 3:
13+
from http.cookies import BaseCookie
14+
from http.server import HTTPServer, BaseHTTPRequestHandler
15+
16+
if not hasattr(__builtins__, "basestring"): basestring = (str, bytes)
17+
1118
from cryptography.fernet import Fernet
1219
from cryptography.fernet import InvalidToken
1320

@@ -20,7 +27,11 @@
2027
# -----------------------------------------------------------------------------
2128
# Requests are processed in separate thread
2229
import threading
23-
from SocketServer import ThreadingMixIn
30+
if sys.version_info.major == 2:
31+
from SocketServer import ThreadingMixIn
32+
elif sys.version_info.major == 3:
33+
from socketserver import ThreadingMixIn
34+
2435
class AuthHTTPServer(ThreadingMixIn, HTTPServer):
2536
pass
2637
# -----------------------------------------------------------------------------
@@ -74,13 +85,16 @@ def do_GET(self):
7485
ctx['action'] = 'decoding credentials'
7586

7687
try:
77-
cipher_suite = Fernet('REPLACEWITHFERNETKEY')
88+
cipher_suite = Fernet(REPLACEWITHFERNETKEY)
7889
self.log_message('Trying to dechipher credentials...')
79-
auth_decoded = cipher_suite.decrypt(auth_header[6:])
90+
auth_decoded = auth_header[6:].encode()
91+
auth_decoded = cipher_suite.decrypt(auth_decoded)
92+
auth_decoded = auth_decoded.decode("utf-8")
8093
user, passwd = auth_decoded.split(':', 1)
8194
except InvalidToken:
8295
self.log_message('Incorrect token. Trying to decode credentials from BASE64...')
8396
auth_decoded = base64.b64decode(auth_header[6:])
97+
auth_decoded = auth_decoded.decode("utf-8")
8498
user, passwd = auth_decoded.split(':', 1)
8599
except Exception as e:
86100
self.auth_failed(ctx)
@@ -96,7 +110,7 @@ def do_GET(self):
96110
def get_cookie(self, name):
97111
cookies = self.headers.get('Cookie')
98112
if cookies:
99-
authcookie = Cookie.BaseCookie(cookies).get(name)
113+
authcookie = BaseCookie(cookies).get(name)
100114
if authcookie:
101115
return authcookie.value
102116
else:

root/etc/cont-init.d/30-config

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/with-contenv bash
22

33
# generate fernet key for ldap if it doesn't exist
4-
[[ $(cat /app/ldap-backend-app.py | grep 'REPLACEWITHFERNETKEY') ]] && \
5-
FERNETKEY=$(python /app/fernet-key.py) && \
6-
sed -i "s/REPLACEWITHFERNETKEY/${FERNETKEY}/" /app/ldap-backend-app.py && \
7-
sed -i "s/REPLACEWITHFERNETKEY/${FERNETKEY}/" /app/nginx-ldap-auth-daemon.py
4+
if grep -q 'REPLACEWITHFERNETKEY' /app/ldap-backend-app.py; then
5+
FERNETKEY=$(python3 /app/fernet-key.py)
6+
sed -i "s/REPLACEWITHFERNETKEY/${FERNETKEY}/" /app/ldap-backend-app.py
7+
sed -i "s/REPLACEWITHFERNETKEY/${FERNETKEY}/" /app/nginx-ldap-auth-daemon.py
8+
echo "generated fernet key"
9+
fi

root/etc/services.d/ldap-app/run

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/usr/bin/with-contenv bash
22
exec \
3-
s6-setuidgid abc python /app/ldap-backend-app.py --host 0.0.0.0 --port 9000
3+
s6-setuidgid abc python3 /app/ldap-backend-app.py --host 0.0.0.0 --port 9000

0 commit comments

Comments
 (0)