Message ID | 20220721114514.52930-1-ayoub.zaki@embexus.com |
---|---|
State | Changes Requested |
Headers | show |
Series | [swugenerator] extract_keys: fix parsing encyption keyfile | expand |
On 21.07.22 13:45, Ayoub Zaki wrote: > Signed-off-by: Ayoub Zaki <ayoub.zaki@embexus.com> > --- > swugenerator/main.py | 18 ++++++++++-------- > 1 file changed, 10 insertions(+), 8 deletions(-) > > diff --git a/swugenerator/main.py b/swugenerator/main.py > index 4fe9dd4..36d629a 100644 > --- a/swugenerator/main.py > +++ b/swugenerator/main.py > @@ -24,14 +24,16 @@ def extract_keys(keyfile): > logging.fatal("Failed to open file with keys %s" % (keyfile)) > exit(1) > > - data = {} > - for _ in lines: > - k,v = _.split() > - data[k.rstrip()] = v I can just full agree that this is definetly wrong. > - > - key = k.rstrip('\n') > - iv = v.rstrip('\n') > - return key, iv > + k, v = None, None > + for line in lines: > + if 'key' in line: > + k = line.split('=')[1] > + if 'iv' in line: > + v = line.split('=')[1] > + if None not in (k, v): > + k = k.rstrip('\n') > + v = v.rstrip('\n') > + return k, v > Anyway, we could maybe simply replace with the same code from meta-swupdate, classes/swupdate-lib.bbclass, swupdate_extract_keys: def swupdate_extract_keys(keyfile_path): try: with open(keyfile_path, 'r') as f: lines = f.readlines() except IOError: bb.fatal("Failed to open file with keys %s" % (keyfile_path)) data = {} for _ in lines: k,v = _.split('=',maxsplit=1) data[k.rstrip()] = v key = data['key'].rstrip('\n') iv = data['iv'].rstrip('\n') return key,iv What do you mind ? Just to avoid to have two different implementation for the same thing. Regards, Stefano
Hi Stefano, it was the first thing I tried, it doen't look working for python3.8 version: Traceback (most recent call last): File "/usr/local/bin/swugenerator", line 8, in <module> sys.exit(main()) File "/usr/local/lib/python3.8/dist-packages/swugenerator/main.py", line 152, in main key, iv = extract_keys(args.encryption_key_file) File "/usr/local/lib/python3.8/dist-packages/swugenerator/main.py", line 29, in extract_keys k,v = _.split('=',maxsplit=1) ValueError: not enough values to unpack (expected 2, got 1) On Friday, August 5, 2022 at 11:11:22 AM UTC+2 Stefano Babic wrote: > On 21.07.22 13:45, Ayoub Zaki wrote: > > Signed-off-by: Ayoub Zaki <ayoub...@embexus.com> > > --- > > swugenerator/main.py | 18 ++++++++++-------- > > 1 file changed, 10 insertions(+), 8 deletions(-) > > > > diff --git a/swugenerator/main.py b/swugenerator/main.py > > index 4fe9dd4..36d629a 100644 > > --- a/swugenerator/main.py > > +++ b/swugenerator/main.py > > @@ -24,14 +24,16 @@ def extract_keys(keyfile): > > logging.fatal("Failed to open file with keys %s" % (keyfile)) > > exit(1) > > > > - data = {} > > - for _ in lines: > > - k,v = _.split() > > - data[k.rstrip()] = v > > I can just full agree that this is definetly wrong. > > > - > > - key = k.rstrip('\n') > > - iv = v.rstrip('\n') > > - return key, iv > > + k, v = None, None > > + for line in lines: > > + if 'key' in line: > > + k = line.split('=')[1] > > + if 'iv' in line: > > + v = line.split('=')[1] > > + if None not in (k, v): > > + k = k.rstrip('\n') > > + v = v.rstrip('\n') > > + return k, v > > > > Anyway, we could maybe simply replace with the same code from > meta-swupdate, classes/swupdate-lib.bbclass, swupdate_extract_keys: > > > def swupdate_extract_keys(keyfile_path): > try: > with open(keyfile_path, 'r') as f: > lines = f.readlines() > except IOError: > bb.fatal("Failed to open file with keys %s" % (keyfile_path)) > > data = {} > for _ in lines: > k,v = _.split('=',maxsplit=1) > data[k.rstrip()] = v > > key = data['key'].rstrip('\n') > iv = data['iv'].rstrip('\n') > > return key,iv > > What do you mind ? Just to avoid to have two different implementation > for the same thing. > > Regards, > Stefano > > -- > ===================================================================== > DENX Software Engineering GmbH, Managing Director: Wolfgang Denk > HRB 165235 Munich, Office: Kirchenstr.5, 82194 Groebenzell, Germany > Phone: +49-8142-66989-53 <+49%208142%206698953> Fax: +49-8142-66989-80 > <+49%208142%206698980> Email: sba...@denx.de > ===================================================================== >
diff --git a/swugenerator/main.py b/swugenerator/main.py index 4fe9dd4..36d629a 100644 --- a/swugenerator/main.py +++ b/swugenerator/main.py @@ -24,14 +24,16 @@ def extract_keys(keyfile): logging.fatal("Failed to open file with keys %s" % (keyfile)) exit(1) - data = {} - for _ in lines: - k,v = _.split() - data[k.rstrip()] = v - - key = k.rstrip('\n') - iv = v.rstrip('\n') - return key, iv + k, v = None, None + for line in lines: + if 'key' in line: + k = line.split('=')[1] + if 'iv' in line: + v = line.split('=')[1] + if None not in (k, v): + k = k.rstrip('\n') + v = v.rstrip('\n') + return k, v def main() -> None:
Signed-off-by: Ayoub Zaki <ayoub.zaki@embexus.com> --- swugenerator/main.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)