3
# Copyright 2014 Canonical Ltd.
5
# This program is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU Affero General Public License version 3, as
7
# published by the Free Software Foundation.
9
# This program is distributed in the hope that it will be useful, but
10
# WITHOUT ANY WARRANTY; without even the implied warranties of
11
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
12
# PURPOSE. See the GNU Affero General Public License for more details.
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
from oauthlib.common import (
21
from argparse import ArgumentParser
23
from textwrap import dedent
30
parser = ArgumentParser('nfss')
31
subparsers = parser.add_subparsers(help='Commands', dest="command")
33
subparsers.add_parser(
35
help="Generate a new set of access keys.",
37
parser_keys_del = subparsers.add_parser(
39
help="Remove an existing set of access keys.",
41
parser_keys_del.add_argument(
43
help="The client access key you want to invalidate."
45
subparsers.add_parser(
47
help="List current client access keys."
49
subparsers.add_parser(
51
help="Migrate the database.",
53
subparsers.add_parser(
55
help="Run database maintenance tasks."
57
arguments = parser.parse_args()
58
if arguments.command is None:
59
parser.error("Missing command string.")
62
if arguments.command == 'keys-add':
64
elif arguments.command == 'keys-del':
65
keys_del(arguments.client_key)
66
elif arguments.command == 'keys-list':
68
elif arguments.command == 'database-migrate':
69
nfss.db_migrate.main()
70
elif arguments.command == 'database-clean':
75
global INSERT_SCRIPT_TEMPLATE
77
name = description = poc = ""
79
name = input("Client name: ")
80
poc = input("Point of contact (name & email address): ")
81
description = input("Description of client: ")
91
""".format(name, poc, description)
94
correct = input("Is this information correct (y/n)? ")
95
repeat = correct.lower().strip() == "n"
97
access_key = generate_token()
98
owner_key = generate_token()
99
owner_secret = generate_client_id()
101
database_connection = nfss.database.get_connection_for_request()
102
nfss.database.auth_add_client_details(
111
database_connection.close()
113
valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)
114
filename = ''.join(c for c in name if c in valid_chars)
115
filename = filename.replace(' ', '_') + '_insert.py'
118
with open(filename, 'w') as f:
120
INSERT_SCRIPT_TEMPLATE.format(
121
client_access_key=access_key,
122
resource_owner_key=owner_key,
123
resource_owner_secret=owner_secret,
127
print("Cannot write to the default location (%s)" % filename)
128
filename = input("Where should the new script be written to? ")
131
"%r has been written. Use this to insert data into the data store." %
137
def keys_del(client_key):
138
with nfss.database.get_scoped_connection() as db_connection:
139
if not nfss.database.get_auth_client_key_exists(
143
print("Error: the client key '%s' does not exist. Use 'keys-list'")
144
print(" to get a list of all the client access keys.")
146
nfss.database.invalidate_client_key(db_connection, client_key)
147
if not nfss.database.get_auth_client_key_exists(
151
print("Client key has been successfully invalidated.")
153
print("Client key has NOT been invalidated.")
157
with nfss.database.get_scoped_connection() as db_connection:
158
for key in nfss.database.get_auth_client_key_list(db_connection):
162
def database_clean():
163
with nfss.database.get_scoped_connection() as db_connection:
164
nfss.database.clean_old_nonces(db_connection)
167
INSERT_SCRIPT_TEMPLATE = r'''#!/usr/bin/env python3
171
from requests_oauthlib import OAuth1Session
173
# Note: The resource_owner_secret is *secret*, and must be kept secret. If
174
# you think it's been compromised, contact IS with your client access key
175
# and they can invalidate the old key and assign a new one.
176
client_access_key = {client_access_key!r}
177
resource_owner_key = {resource_owner_key!r}
178
resource_owner_secret = {resource_owner_secret!r}
179
backend = 'http://nfss.ubuntu.com/api/v1'
181
if len(sys.argv) != 3:
182
print("Usage: %s <projectname> <testname>\n" % sys.argv[0])
183
print("Pipe json test data to this script to insert it into the database.")
186
project = sys.argv[1]
189
if sys.stdin.isatty():
190
print("Error: Pipe json test data through this script, like so:")
191
print("$ cat test_data | %s %s %s" % (sys.argv[0], project, test))
194
data = sys.stdin.read()
197
except ValueError as e:
198
print("Error: Data does not appear to be valid json: %s" % e)
201
test_session = OAuth1Session(
203
resource_owner_key=resource_owner_key,
204
resource_owner_secret=resource_owner_secret
206
url = '/'.join((backend, project, test))
207
r = test_session.post(url, dict(data=data.encode()))
212
if __name__ == '__main__':