test_write_tls_tunnel_state_file.py 5.08 KB
Newer Older
Max Beckett's avatar
Max Beckett committed
1
2
3
4
5
6
7
8
9
10
11
12
#
# Copyright 2017-2018 Amazon.com, Inc. and its affiliates. All Rights Reserved.
#
# Licensed under the MIT License. See the LICENSE accompanying this file
# for the specific language governing permissions and limitations under
# the License.
#

import mount_efs
import json
import os

13
14
from datetime import datetime

Max Beckett's avatar
Max Beckett committed
15
16
17
18
FS_ID = 'fs-deadbeef'
PID = 1234
PORT = 54323
COMMAND = ['stunnel', '/some/config/file']
19
20
NETNS = '/proc/1/net/ns'
NETNS_COMMAND = ['nsenter', '--net=' + NETNS] + COMMAND
Max Beckett's avatar
Max Beckett committed
21
FILES = ['/tmp/foo', '/tmp/bar']
22
DATETIME_FORMAT = '%y%m%d%H%M%SZ'
Max Beckett's avatar
Max Beckett committed
23
24


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def test_write_tls_tunnel_state_file_netns(tmpdir):
    state_file_dir = str(tmpdir)

    mount_point = '/home/user/foo/mount'

    current_time = datetime.utcnow()
    cert_creation_time = current_time.strftime(DATETIME_FORMAT)

    cert_details = {
        'accessPoint': 'fsap-fedcba9876543210',
        'certificate': '/tmp/baz',
        'privateKey': '/tmp/key.pem',
        'mountStateDir': 'fs-deadbeef.mount.dir.12345',
        'commonName': 'fs-deadbeef.efs.us-east-1.amazonaws.com',
        'region': 'us-east-1',
        'fsId': FS_ID,
        'certificateCreationTime': cert_creation_time,
        'useIam': True
    }

    state_file = mount_efs.write_tls_tunnel_state_file(FS_ID, mount_point, PORT, PID, NETNS_COMMAND, FILES,
                                                       state_file_dir, cert_details)

    assert FS_ID in state_file
    assert os.sep not in state_file[state_file.find(FS_ID):]

    assert os.path.exists(state_file_dir)

    state_file = os.path.join(state_file_dir, state_file)
    assert os.path.exists(state_file)

    with open(state_file) as f:
        state = json.load(f)

    assert PID == state.get('pid')
    assert NETNS_COMMAND == state.get('cmd')
    assert FILES == state.get('files')
    assert cert_details['commonName'] == state.get('commonName')
    assert cert_details['certificate'] == state.get('certificate')
    assert cert_details['certificateCreationTime'] == state.get('certificateCreationTime')
    assert cert_details['mountStateDir'] == state.get('mountStateDir')
    assert cert_details['privateKey'] == state.get('privateKey')
    assert cert_details['region'] == state.get('region')
    assert cert_details['accessPoint'] == state.get('accessPoint')
    assert cert_details['fsId'] == state.get('fsId')
    assert cert_details['useIam'] == state.get('useIam')


Max Beckett's avatar
Max Beckett committed
73
74
75
76
77
def test_write_tls_tunnel_state_file(tmpdir):
    state_file_dir = str(tmpdir)

    mount_point = '/home/user/foo/mount'

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    current_time = datetime.utcnow()
    cert_creation_time = current_time.strftime(DATETIME_FORMAT)

    cert_details = {
        'accessPoint': 'fsap-fedcba9876543210',
        'certificate': '/tmp/baz',
        'privateKey': '/tmp/key.pem',
        'mountStateDir': 'fs-deadbeef.mount.dir.12345',
        'commonName': 'fs-deadbeef.efs.us-east-1.amazonaws.com',
        'region': 'us-east-1',
        'fsId': FS_ID,
        'certificateCreationTime': cert_creation_time,
        'useIam': True
    }

    state_file = mount_efs.write_tls_tunnel_state_file(FS_ID, mount_point, PORT, PID, COMMAND, FILES, state_file_dir,
                                                       cert_details)

    assert FS_ID in state_file
    assert os.sep not in state_file[state_file.find(FS_ID):]

    assert os.path.exists(state_file_dir)

    state_file = os.path.join(state_file_dir, state_file)
    assert os.path.exists(state_file)

    with open(state_file) as f:
        state = json.load(f)

    assert PID == state.get('pid')
    assert COMMAND == state.get('cmd')
    assert FILES == state.get('files')
    assert cert_details['commonName'] == state.get('commonName')
    assert cert_details['certificate'] == state.get('certificate')
    assert cert_details['certificateCreationTime'] == state.get('certificateCreationTime')
    assert cert_details['mountStateDir'] == state.get('mountStateDir')
    assert cert_details['privateKey'] == state.get('privateKey')
    assert cert_details['region'] == state.get('region')
    assert cert_details['accessPoint'] == state.get('accessPoint')
    assert cert_details['fsId'] == state.get('fsId')
    assert cert_details['useIam'] == state.get('useIam')


def test_write_tls_tunnel_state_file_no_cert(tmpdir):
    state_file_dir = str(tmpdir)

    mount_point = '/home/user/foo/mount'

Max Beckett's avatar
Max Beckett committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
    state_file = mount_efs.write_tls_tunnel_state_file(FS_ID, mount_point, PORT, PID, COMMAND, FILES, state_file_dir)

    assert FS_ID in state_file
    assert os.sep not in state_file[state_file.find(FS_ID):]

    assert os.path.exists(state_file_dir)

    state_file = os.path.join(state_file_dir, state_file)
    assert os.path.exists(state_file)

    with open(state_file) as f:
        state = json.load(f)

    assert PID == state.get('pid')
    assert COMMAND == state.get('cmd')
    assert FILES == state.get('files')
142
143
144
145
146
147
148
149
150
    assert 'commonName' not in state
    assert 'certificate' not in state
    assert 'certificateCreationTime' not in state
    assert 'mountStateDir' not in state
    assert 'privateKey' not in state
    assert 'region' not in state
    assert 'accessPoint' not in state
    assert 'fsId' not in state
    assert 'useIam' not in state