Skip to content

Commit f7e7caa

Browse files
authored
Merge pull request #12 from securenative/SN-1556-align-sdk
Sn 1556 align sdk
2 parents a4666a7 + f2444b8 commit f7e7caa

14 files changed

Lines changed: 355 additions & 215 deletions

.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/context/context_builder.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,16 @@ def default_context_builder():
4242

4343
@staticmethod
4444
def from_http_request(request):
45-
headers = request.headers
45+
try:
46+
client_token = request.cookies[RequestUtils.SECURENATIVE_COOKIE]
47+
except AttributeError:
48+
client_token = None
49+
50+
try:
51+
headers = request.headers
52+
except AttributeError:
53+
headers = None
4654

47-
client_token = request.cookies[RequestUtils.SECURENATIVE_COOKIE]
4855
if Utils.is_null_or_empty(client_token):
4956
client_token = RequestUtils.get_secure_header_from_request(headers)
5057

securenative/event_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def send_sync(self, event, resource_path, retry):
7777
json.dumps(EventManager.serialize(event)),
7878
retry
7979
)
80-
self.queue.append(0, item)
80+
self.queue.append(item)
8181
if self._is_queue_full():
8282
self.queue = self.queue[:len(self.queue - 1)]
8383
return res
@@ -136,7 +136,7 @@ def stop_event_persist(self):
136136
def serialize(obj):
137137
return {
138138
"rid": obj.rid,
139-
"eventType": obj.event_type.value,
139+
"eventType": obj.event_type,
140140
"userId": obj.user_id,
141141
"userTraits": {
142142
"name": obj.user_traits.name,

securenative/models/sdk_event.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ def __init__(self, event_options, securenative_options):
2121
self.user_id = event_options.user_id
2222
self.user_traits = event_options.user_traits
2323
self.request = RequestContextBuilder() \
24-
.with_cid(client_token.cid) \
25-
.with_vid(client_token.vid) \
26-
.with_fp(client_token.fp) \
24+
.with_cid(client_token.cid if client_token else "") \
25+
.with_vid(client_token.vid if client_token else "") \
26+
.with_fp(client_token.fp if client_token else "") \
2727
.with_ip(self.context.ip) \
2828
.with_remote_ip(self.context.remote_ip) \
2929
.with_method(self.context.method) \

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/request_utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ class RequestUtils(object):
44

55
@staticmethod
66
def get_secure_header_from_request(headers):
7-
return headers[RequestUtils.SECURENATIVE_HEADER]
7+
if headers:
8+
return headers[RequestUtils.SECURENATIVE_HEADER]
9+
return []
810

911
@staticmethod
1012
def get_client_ip_from_request(request):
@@ -17,4 +19,7 @@ def get_client_ip_from_request(request):
1719

1820
@staticmethod
1921
def get_remote_ip_from_request(request):
20-
return request.raw._original_response.fp.raw._sock.getpeername()[0]
22+
try:
23+
return request.raw._original_response.fp.raw._sock.getpeername()[0]
24+
except AttributeError:
25+
return ""

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)