Fix encrypt_secret for python3
This fixes the encrypt_secret tool for use with python3. This needs some minor changes to imports, encodings and base64 encoding. Change-Id: Id29ebedab2115d0d5d47049f2a0412e8c75aa8ef
This commit is contained in:
parent
a7cf5c518a
commit
39d6dcd180
|
@ -13,11 +13,19 @@
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
import base64
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
import urllib
|
|
||||||
|
# we to import Request and urlopen differently for python 2 and 3
|
||||||
|
try:
|
||||||
|
from urllib.request import Request
|
||||||
|
from urllib.request import urlopen
|
||||||
|
except ImportError:
|
||||||
|
from urllib2 import Request
|
||||||
|
from urllib2 import urlopen
|
||||||
|
|
||||||
DESCRIPTION = """Encrypt a secret for Zuul.
|
DESCRIPTION = """Encrypt a secret for Zuul.
|
||||||
|
|
||||||
|
@ -50,9 +58,9 @@ def main():
|
||||||
"to standard output.")
|
"to standard output.")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
req = urllib.request.Request("%s/keys/%s/%s.pub" % (
|
req = Request("%s/keys/%s/%s.pub" % (
|
||||||
args.url, args.source, args.project))
|
args.url, args.source, args.project))
|
||||||
pubkey = urllib.request.urlopen(req)
|
pubkey = urlopen(req)
|
||||||
|
|
||||||
if args.infile:
|
if args.infile:
|
||||||
with open(args.infile) as f:
|
with open(args.infile) as f:
|
||||||
|
@ -70,18 +78,18 @@ def main():
|
||||||
pubkey_file.name],
|
pubkey_file.name],
|
||||||
stdin=subprocess.PIPE,
|
stdin=subprocess.PIPE,
|
||||||
stdout=subprocess.PIPE)
|
stdout=subprocess.PIPE)
|
||||||
(stdout, stderr) = p.communicate(plaintext)
|
(stdout, stderr) = p.communicate(plaintext.encode("utf-8"))
|
||||||
if p.returncode != 0:
|
if p.returncode != 0:
|
||||||
raise Exception("Return code %s from openssl" % p.returncode)
|
raise Exception("Return code %s from openssl" % p.returncode)
|
||||||
ciphertext = stdout.encode('base64')
|
ciphertext = base64.b64encode(stdout)
|
||||||
finally:
|
finally:
|
||||||
os.unlink(pubkey_file.name)
|
os.unlink(pubkey_file.name)
|
||||||
|
|
||||||
if args.outfile:
|
if args.outfile:
|
||||||
with open(args.outfile, "w") as f:
|
with open(args.outfile, "wb") as f:
|
||||||
f.write(ciphertext)
|
f.write(ciphertext)
|
||||||
else:
|
else:
|
||||||
print(ciphertext)
|
print(ciphertext.decode("utf-8"))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in New Issue