Skip to content

Commit d447a4a

Browse files
author
Inbal Tako
committed
Add fault catch for decrypt and github actions
1 parent aa337f4 commit d447a4a

5 files changed

Lines changed: 205 additions & 15 deletions

File tree

.github/workflows/ci.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- master
7+
- dev
8+
- dev-*
9+
10+
jobs:
11+
ci:
12+
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Notify slack success
16+
if: success()
17+
id: slack # IMPORTANT: reference this step ID value in future Slack steps
18+
env:
19+
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
20+
uses: voxmedia/github-action-slack-notify-build@v1.1.1
21+
with:
22+
channel: github-actions
23+
status: STARTING
24+
color: warning
25+
26+
- name: Publish a Python distribution to PyPI
27+
uses: pypa/gh-action-pypi-publish@master
28+
with:
29+
user: __token__
30+
password: ${{ secrets.pypi_password }}
31+
32+
- name: Run Tests
33+
run: python -m unittest
34+
35+
- name: Notify slack success
36+
if: success()
37+
env:
38+
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
39+
uses: voxmedia/github-action-slack-notify-build@v1.1.1
40+
with:
41+
message_id: ${{ steps.slack.outputs.message_id }}
42+
channel: github-actions
43+
status: SUCCESS
44+
color: good
45+
46+
- name: Notify slack fail
47+
if: failure()
48+
env:
49+
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
50+
uses: voxmedia/github-action-slack-notify-build@v1.1.1
51+
with:
52+
message_id: ${{ steps.slack.outputs.message_id }}
53+
channel: github-actions
54+
status: FAILED
55+
color: danger

.github/workflows/publish.yml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
name: Publish
2+
3+
on:
4+
release:
5+
types: [created]
6+
7+
jobs:
8+
release:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Notify Starting
12+
if: success()
13+
id: slack # IMPORTANT: reference this step ID value in future Slack steps
14+
env:
15+
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
16+
uses: voxmedia/github-action-slack-notify-build@v1.1.1
17+
with:
18+
channel: github-actions
19+
status: STARTING
20+
color: warning
21+
22+
- uses: actions/checkout@v2
23+
- name: Set up Maven Central Repository
24+
uses: actions/setup-java@v1
25+
with:
26+
java-version: 1.8
27+
server-id: ossrh
28+
server-username: MAVEN_USERNAME
29+
server-password: MAVEN_PASSWORD
30+
31+
- name: Build
32+
run: mvn -B package --file pom.xml
33+
34+
- name: Run Tests
35+
run: mvn test
36+
37+
- name: Release Maven package
38+
uses: samuelmeuli/action-maven-publish@v1
39+
with:
40+
gpg_private_key: ${{ secrets.gpg_private_key }}
41+
gpg_passphrase: ${{ secrets.gpg_passphrase }}
42+
nexus_username: ${{ secrets.nexus_username }}
43+
nexus_password: ${{ secrets.nexus_password }}
44+
45+
- name: Notify slack success
46+
if: success()
47+
env:
48+
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
49+
uses: voxmedia/github-action-slack-notify-build@v1.1.1
50+
with:
51+
message_id: ${{ steps.slack.outputs.message_id }}
52+
channel: github-actions
53+
status: SUCCESS
54+
color: good
55+
56+
- name: Notify slack fail
57+
if: failure()
58+
env:
59+
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
60+
uses: voxmedia/github-action-slack-notify-build@v1.1.1
61+
with:
62+
message_id: ${{ steps.slack.outputs.message_id }}
63+
channel: github-actions
64+
status: FAILED
65+
color: danger

.github/workflows/test.yml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
name: Build
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
- dev
8+
- dev-*
9+
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Notify slack success
15+
if: success()
16+
id: slack # IMPORTANT: reference this step ID value in future Slack steps
17+
env:
18+
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
19+
uses: voxmedia/github-action-slack-notify-build@v1.1.1
20+
with:
21+
channel: github-actions
22+
status: STARTING
23+
color: warning
24+
25+
- uses: actions/checkout@v2
26+
- name: Set up JDK 1.8
27+
uses: actions/setup-java@v1
28+
with:
29+
java-version: 1.8
30+
31+
- name: Build
32+
run: mvn -B package --file pom.xml
33+
34+
- name: Run Tests
35+
run: mvn test
36+
37+
- name: Upload coverage to Codecov
38+
uses: codecov/codecov-action@v1
39+
with:
40+
token: ${{ secrets.CODECOV_TOKEN }}
41+
flags: unittests
42+
name: codecov-umbrella
43+
fail_ci_if_error: true
44+
45+
- name: Notify slack success
46+
if: success()
47+
env:
48+
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
49+
uses: voxmedia/github-action-slack-notify-build@v1.1.1
50+
with:
51+
message_id: ${{ steps.slack.outputs.message_id }}
52+
channel: github-actions
53+
status: SUCCESS
54+
color: good
55+
56+
- name: Notify slack fail
57+
if: failure()
58+
env:
59+
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
60+
uses: voxmedia/github-action-slack-notify-build@v1.1.1
61+
with:
62+
message_id: ${{ steps.slack.outputs.message_id }}
63+
channel: github-actions
64+
status: FAILED
65+
color: danger

securenative/utils/encryption_utils.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import json
22
from binascii import unhexlify, hexlify
3-
from json import JSONDecodeError
43

54
from Crypto import Random
65
from Crypto.Cipher import AES
76

7+
from securenative.logger import Logger
88
from securenative.models.client_token import ClientToken
99

1010

@@ -14,25 +14,30 @@ class EncryptionUtils(object):
1414

1515
@classmethod
1616
def encrypt(cls, text, cipher_key):
17-
key = cipher_key[:cls.KEY_SIZE]
18-
iv = Random.new().read(AES.block_size)
19-
cipher = AES.new(key, AES.MODE_CBC, iv)
20-
raw = str(cls._pad(text))
21-
return hexlify(iv + cipher.encrypt(raw))
17+
try:
18+
key = cipher_key[:cls.KEY_SIZE]
19+
iv = Random.new().read(AES.block_size)
20+
cipher = AES.new(key, AES.MODE_CBC, iv)
21+
raw = str(cls._pad(text))
22+
return hexlify(iv + cipher.encrypt(raw))
23+
except Exception as e:
24+
Logger.error("Could not encrypt text {}; {}".format(text, e))
25+
return None
2226

2327
@classmethod
2428
def decrypt(cls, encrypted, cipher_key):
25-
key = cipher_key[:cls.KEY_SIZE]
26-
content = unhexlify(encrypted)
27-
iv = content[:cls.BLOCK_SIZE]
28-
cipher_text = content[cls.BLOCK_SIZE:]
29-
aes = AES.new(key, AES.MODE_CBC, iv)
30-
rv = aes.decrypt(cipher_text).decode("utf-8").strip()
3129
try:
30+
key = cipher_key[:cls.KEY_SIZE]
31+
content = unhexlify(encrypted)
32+
iv = content[:cls.BLOCK_SIZE]
33+
cipher_text = content[cls.BLOCK_SIZE:]
34+
aes = AES.new(key, AES.MODE_CBC, iv)
35+
rv = aes.decrypt(cipher_text).decode("utf-8").strip()
3236
secret = json.loads(rv)
3337
return ClientToken(secret.get("cid"), secret.get("vid"), secret.get("fp"))
34-
except JSONDecodeError:
35-
return rv
38+
except Exception as e:
39+
Logger.error("Could not decrypt str {}; {}".format(encrypted, e))
40+
return None
3641

3742
@classmethod
3843
def _pad(cls, s):

securenative/utils/version_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ class VersionUtils(object):
22

33
@staticmethod
44
def get_version():
5-
return "0.1.8"
5+
return "0.1.9"

0 commit comments

Comments
 (0)