-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblih
executable file
·309 lines (272 loc) · 11.2 KB
/
blih
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
73
74
75
76
77
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/python3
#-
# Copyright 2013-2014 Emmanuel Vadot <[email protected]>
# All rights reserved
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted providing that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import os
import sys
import getopt
import hmac
import hashlib
import urllib.request
import urllib.parse
import json
import getpass
version = 1.7
class blih:
def __init__(self, baseurl='https://blih.epitech.eu/', user=None, token=None, verbose=False, user_agent='blih-' + str(version)):
self._baseurl = baseurl
if token:
self._token = token
else:
self.token_calc()
if user == None:
self._user = getpass.getuser()
else:
self._user = user
self._verbose = verbose
self._useragent = user_agent
def token_get(self):
return self._token
def token_set(self, token):
self._token = token
token = property(token_get, token_set)
def token_calc(self):
self._token = bytes(hashlib.sha512(bytes(getpass.getpass(), 'utf8')).hexdigest(), 'utf8')
def sign_data(self, data=None):
signature = hmac.new(self._token, msg=bytes(self._user, 'utf8'), digestmod=hashlib.sha512)
if data:
signature.update(bytes(json.dumps(data, sort_keys=True, indent=4, separators=(',', ': ')), 'utf8'))
signed_data = {'user' : self._user, 'signature' : signature.hexdigest()}
if data != None:
signed_data['data'] = data
return signed_data
def request(self, resource, method='GET', content_type='application/json', data=None, url=None):
signed_data = self.sign_data(data)
if url:
req = urllib.request.Request(url=url, method=method, data=bytes(json.dumps(signed_data), 'utf8'))
else:
req = urllib.request.Request(url=self._baseurl + resource, method=method, data=bytes(json.dumps(signed_data), 'utf8'))
req.add_header('Content-Type', content_type)
req.add_header('User-Agent', self._useragent)
try:
f = urllib.request.urlopen(req)
except urllib.error.HTTPError as e:
print ('HTTP Error ' + str(e.code))
data = json.loads(e.read().decode('utf8'))
print ("Error message : '" + data['error'] + "'")
sys.exit(1)
if f.status == 200:
try:
data = json.loads(f.read().decode('utf8'))
except:
print ("Can't decode data, aborting")
sys.exit(1)
return (f.status, f.reason, f.info(), data)
print ('Unknown error')
sys.exit(1)
def repo_create(self, name, type='git', description=None):
data = {'name' : name, 'type' : type}
if description:
data['description'] = description
status, reason, headers, data = self.request('/repositories', method='POST', data=data)
print (data['message'])
def repo_list(self):
status, reason, headers, data = self.request('/repositories', method='GET')
for i in data['repositories']:
print (i)
def repo_delete(self, name):
status, reason, headers, data = self.request('/repository/' + name, method='DELETE')
print (data['message'])
def repo_info(self, name):
status, reason, headers, data = self.request('/repository/' + name, method='GET')
print (data['message'])
def repo_setacl(self, name, acluser, acl):
data = {'user' : acluser, 'acl' : acl}
status, reason, headers, data = self.request('/repository/' + name + '/acls', method='POST', data=data)
print (data['message'])
def repo_getacl(self, name):
status, reason, headers, data = self.request('/repository/' + name + '/acls', method='GET')
for i in data.keys():
print (i + ':' + data[i])
def sshkey_upload(self, keyfile):
try:
f = open(keyfile, 'r')
except (PermissionError, FileNotFoundError):
print ("Can't open file : " + keyfile)
return
key = urllib.parse.quote(f.read().strip('\n'))
f.close()
data = {'sshkey' : key}
status, reason, headers, data = self.request('/sshkeys', method='POST', data=data)
print (data['message'])
def sshkey_delete(self, sshkey):
status, reason, headers, data = self.request('/sshkey/' + sshkey, method='DELETE')
print (data['message'])
def sshkey_list(self):
status, reason, headers, data = self.request('/sshkeys', method='GET')
for i in data.keys():
print (data[i] + ' ' + i)
def whoami(self):
status, reason, headers, data = self.request('/whoami', method='GET')
print (data['message'])
def usage_repository():
print ('Usage: ' + sys.argv[0] + ' [options] repository command ...')
print ()
print ('Commands :')
print ('\tcreate repo\t\t\t-- Create a repository named "repo"')
print ('\tinfo repo\t\t\t-- Get the repository metadata')
print ('\tgetacl repo\t\t\t-- Get the acls set for the repository')
print ('\tlist\t\t\t\t-- List the repositories created')
print ('\tsetacl repo user [acl]\t\t-- Set (or remove) an acl for "user" on "repo"')
print ('\t\t\t\t\tACL format:')
print ('\t\t\t\t\tr for read')
print ('\t\t\t\t\tw for write')
print ('\t\t\t\t\ta for admin')
sys.exit(1)
def repository(args, baseurl, user, token, verbose, user_agent):
if len(args) == 0:
usage_repository()
if args[0] == 'create':
if len(args) != 2:
usage_repository()
handle = blih(baseurl=baseurl, user=user, token=token, verbose=verbose, user_agent=user_agent)
handle.repo_create(args[1])
elif args[0] == 'list':
if len(args) != 1:
usage_repository()
handle = blih(baseurl=baseurl, user=user, token=token, verbose=verbose, user_agent=user_agent)
handle.repo_list()
elif args[0] == 'info':
if len(args) != 2:
usage_repository()
handle = blih(baseurl=baseurl, user=user, token=token, verbose=verbose, user_agent=user_agent)
handle.repo_info(args[1])
elif args[0] == 'delete':
if len(args) != 2:
usage_repository()
handle = blih(baseurl=baseurl, user=user, token=token, verbose=verbose, user_agent=user_agent)
handle.repo_delete(args[1])
elif args[0] == 'setacl':
if len(args) != 4 and len(args) != 3:
usage_repository()
if len(args) == 3:
acl = ''
else:
acl = args[3]
handle = blih(baseurl=baseurl, user=user, token=token, verbose=verbose, user_agent=user_agent)
handle.repo_setacl(args[1], args[2], acl)
elif args[0] == 'getacl':
if len(args) != 2:
usage_repository()
handle = blih(baseurl=baseurl, user=user, token=token, verbose=verbose, user_agent=user_agent)
handle.repo_getacl(args[1])
else:
usage_repository()
def usage_sshkey():
print ('Usage: ' + sys.argv[0] + ' [options] sshkey command ...')
print ()
print ('Commands :')
print ('\tupload [file]\t\t\t-- Upload a new ssh-key')
print ('\tlist\t\t\t\t-- List the ssh-keys')
print ('\tdelete <sshkey>\t\t\t-- Delete the sshkey with comment <sshkey>')
sys.exit(1)
def sshkey(args, baseurl, user, token, verbose, user_agent):
if len(args) == 0:
usage_sshkey()
if args[0] == 'list':
handle = blih(baseurl=baseurl, user=user, token=token, verbose=verbose, user_agent=user_agent)
handle.sshkey_list()
elif args[0] == 'upload':
key = None
if len(args) == 1:
key = os.getenv('HOME') + '/.ssh/id_rsa.pub'
elif len(args) == 2:
key = args[1]
else:
usage_sshkey()
handle = blih(baseurl=baseurl, user=user, token=token, verbose=verbose, user_agent=user_agent)
handle.sshkey_upload(key)
elif args[0] == 'delete':
if len(args) != 2:
usage_sshkey()
handle = blih(baseurl=baseurl, user=user, token=token, verbose=verbose, user_agent=user_agent)
handle.sshkey_delete(args[1])
else:
usage_sshkey()
def whoami(args, baseurl, user, token, verbose, user_agent):
handle = blih(baseurl=baseurl, user=user, token=token, verbose=verbose, user_agent=user_agent)
handle.whoami()
def usage():
print ('Usage: ' + sys.argv[0] + ' [options] command ...')
print ()
print ('Global Options :')
print ('\t-u user | --user=user\t\t-- Run as user')
print ('\t-v | --verbose\t\t\t-- Verbose')
print ('\t-b url | --baseurl=url\t\t-- Base URL for BLIH')
print ('\t-t | --token\t\t\t-- Specify token in the cmdline')
print ()
print ('Commands :')
print ('\trepository\t\t\t-- Repository management')
print ('\tsshkey\t\t\t\t-- SSH-KEYS management')
print ('\twhoami\t\t\t\t-- Print who you are')
sys.exit(1)
if __name__ == "__main__":
try:
opts, args = getopt.getopt(sys.argv[1:], 'hvu:b:t:VU:', ['help', 'verbose', 'user=', 'baseurl=', 'token=', 'version', 'useragent='])
except getopt.GetoptError as e:
print (e)
usage()
verbose = False
user = None
baseurl = 'https://blih.epitech.eu/'
token = None
user_agent = 'blih-' + str(version)
for o, a in opts:
if o in ('-h', '--help'):
usage()
elif o in ('-v', '--verbose'):
verbose = True
elif o in ('-u', '--user'):
user = a
elif o in ('-b', '--baseurl'):
baseurl = a
elif o in ('-t', '--token'):
token = bytes(a, 'utf8')
elif o in ('-V', '--version'):
print ('blih version ' + str(version))
sys.exit(0)
elif o in ('-U', '--useragent'):
user_agent = a
else:
usage()
if len(args) == 0:
usage()
if args[0] == 'repository':
repository(args[1:], baseurl, user, token, verbose, user_agent)
elif args[0] == 'sshkey':
sshkey(args[1:], baseurl, user, token, verbose, user_agent)
elif args[0] == 'whoami':
whoami(args[1:], baseurl, user, token, verbose, user_agent)
else:
usage()