comparison tf_apikey_mutate/tf_apikey_mutate.xml @ 1:0183cad9d13b draft

planemo upload
author fubar
date Thu, 22 Feb 2024 10:48:01 +0000
parents
children
comparison
equal deleted inserted replaced
0:2beaae16651e 1:0183cad9d13b
1 <tool name="tf_apikey_mutate" id="tf_apikey_mutate" version="0.001">
2 <!--Source in git at: https://github.com/fubar2/galaxy-->
3 <!--Created by toolfactory@galaxy.org at 21/05/2023 10:01:12 using the Galaxy Tool Factory.-->
4 <description>Rotates all API keys in a ToolFactory instance </description>
5 <requirements>
6 <requirement version="1.1.1" type="package">bioblend</requirement>
7 <requirement version="3.10.12" type="package">python</requirement>
8 <requirement type="package">six</requirement>
9 </requirements>
10 <stdio>
11 <exit_code range="1:" level="fatal"/>
12 </stdio>
13 <version_command><![CDATA[echo "0.001"]]></version_command>
14 <command><![CDATA[python
15 $runme --galaxy_root "$__root_dir__" --galaxy_venv "$__root_dir__/.venv"
16 >
17 $APIK_mutate_log]]></command>
18 <configfiles>
19 <configfile name="runme"><![CDATA[#raw
20
21 #!/usr/bin/env python
22 import argparse
23 import hashlib
24 import os
25 import random
26 import subprocess
27 import sys
28 from time import sleep
29 from urllib import request
30 from urllib.error import URLError
31
32 from bioblend import galaxy
33
34 def add_user(sa_session, security_agent, email, password, key=None, username="admin"):
35 """
36 Add Galaxy User.
37 From John https://gist.github.com/jmchilton/4475646
38 """
39 query = sa_session.query(User).filter_by(email=email)
40 user = None
41 uexists = False
42 User.use_pbkdf2 = False
43 if query.count() > 0:
44 user = query.first()
45 user.username = username
46 user.set_password_cleartext(password)
47 sa_session.add(user)
48 sa_session.flush()
49 uexists = True
50 else:
51 user = User(email)
52 user.username = username
53 user.set_password_cleartext(password)
54 sa_session.add(user)
55 sa_session.flush()
56
57 security_agent.create_private_user_role(user)
58 if not user.default_permissions:
59 security_agent.user_set_default_permissions(user, history=True, dataset=True)
60
61 if key is not None:
62 query = sa_session.query(APIKeys).filter_by(user_id=user.id).delete()
63 sa_session.flush()
64
65 api_key = APIKeys()
66 api_key.user_id = user.id
67 api_key.key = key
68 sa_session.add(api_key)
69 sa_session.flush()
70 return user, uexists
71
72 def run_sed(options):
73 """
74 eg replacement = 'APIK="%s"' % options.key
75 line_start = 'APIK='
76 """
77 fixme = []
78 tool_config_file: "tool_conf.xml,../local_tools/local_tool_conf.xml"
79 # database_connection: "sqlite:///<data_dir>/universe.sqlite?isolation_level=IMMEDIATE"
80 tfc = 'tool_conf.xml,%s/local_tools/local_tool_conf.xml' % options.galaxy_root
81 fixfile = "%s/config/galaxy.yml" % options.galaxy_root
82 fixme.append((' virtualenv: ', ' virtualenv: "%s"' % options.galaxy_venv, fixfile))
83 fixme.append((' galaxy_root: ', ' galaxyroot: "%s"' % options.galaxy_root, fixfile))
84 fixme.append((' tool_config_file: ', ' tool_config_file: "%s"' % tfc, fixfile))
85 fixfile = "%s/local_tools/toolfactory/toolfactory.py" % options.galaxy_root
86 fixme.append((' self.GALAXY_ADMIN_KEY =', ' self.GALAXY_ADMIN_KEY = "%s"' % options.key, fixfile ))
87 fixme.append((' self.GALAXY_URL = ' , ' self.GALAXY_URL = "%s"' % options.galaxy_url, fixfile ))
88 fixfile = "%s/local_tools/toolfactory/install_tf_deps.sh" % options.galaxy_root
89 fixme.append(('APIK=', 'APIK="%s"' % options.key, fixfile ))
90 fixme.append(('LOCALTOOLDIR=', 'LOCALTOOLDIR="%s"' % os.path.join(os.path.abspath(options.galaxy_root), "local_tools"), fixfile ))
91 fixfile = "%s/local_tools/toolfactory/localplanemotest.sh" % options.galaxy_root
92 fixme.append(('GALAXY_URL=', 'GALAXY_URL=%s' % options.galaxy_url, fixfile))
93 fixme.append(('API_KEY=', 'API_KEY=%s' % options.key, fixfile))
94 fixfile = "%s/local_tools/toolfactory/toolfactory_fast_test.sh" % options.galaxy_root
95 fixme.append(('GALAXY_URL=', 'GALAXY_URL=%s' % options.galaxy_url, fixfile))
96 fixme.append(('API_KEY=', 'API_KEY=%s' % options.key, fixfile))
97 fixme.append(('GALAXY_VENV=', 'GALAXY_VENV=%s' % options.galaxy_venv, fixfile))
98 fixme.append(('API_KEY_USER=', 'API_KEY_USER=%s' % options.botkey, fixfile))
99 for line_start, line_replacement, file_to_edit in fixme:
100 cmd = ["sed", "-i", "s#.*%s.*#%s#g" % (line_start, line_replacement), file_to_edit]
101 print("## executing", ' '.join(cmd))
102 res = subprocess.run(cmd)
103 if not res.returncode == 0:
104 print('### Non zero %d return code from %s ' % (res.returncode, ''.join(cmd)))
105
106
107 if __name__ == "__main__":
108 print('starting!', file=sys.stderr)
109 apikey = "%s" % hash(random.random())
110 apikey2 = "%s" % hash(random.random())
111 parser = argparse.ArgumentParser(description="Create Galaxy Admin User.")
112 parser.add_argument("--galaxy_url", help="Galaxy server URL", default="http://localhost:8080")
113 parser.add_argument("--galaxy_root", help="Galaxy root directory path", default="/work/galaxytf")
114 parser.add_argument("--galaxy_venv", help="Galaxy venv path", default="/work/galaxytf/.venv")
115 parser.add_argument("--user", help="Username - an email address.", default="toolfactory@galaxy.org")
116 parser.add_argument("--password", help="Password", default="ChangeMe!")
117 parser.add_argument("--password2", help="Password", default=apikey2)
118 parser.add_argument("--key", help="API-Key.", default=apikey)
119 parser.add_argument("--botkey", help="bot API-Key.", default=apikey2)
120 parser.add_argument("--username", default="tfadmin")
121 parser.add_argument("args", nargs=argparse.REMAINDER)
122 options = parser.parse_args()
123 sys.path.insert(1, options.galaxy_root)
124 sys.path.insert(1, os.path.join(options.galaxy_root, "lib"))
125 sys.path.insert(1, os.path.join(options.galaxy_venv, "lib", "python3.10", "site-packages"))
126 from galaxy.model import User, APIKeys
127 from galaxy.model.mapping import init
128 from galaxy.model.orm.scripts import get_config
129 cnf = get_config(argv=['-c','galaxy', ],cwd=options.galaxy_root)
130 print('cnf=%s' % cnf, file=sys.stderr)
131 cdb_url = cnf["db_url"]
132 # or perhaps "postgresql:///ubuntu?host=/var/run/postgresql"
133 # this is harder to please get_config(sys.argv, use_argparse=False)["db_url"]
134 print('### Using cdb_url', cdb_url, file=sys.stderr)
135 mapping = init("/tmp/", cdb_url)
136 sa_session = mapping.context
137 security_agent = mapping.security_agent
138 usr, uexists = add_user(
139 sa_session, security_agent, options.user, options.password, key=options.key, username=options.username
140 )
141 print("added user", options.user, "apikey", options.key, file=sys.stderr)
142
143 usr, uexists = add_user(
144 sa_session, security_agent, 'test@bx.psu.edu', options.password2, key=options.botkey, username='bot'
145 )
146 run_sed(options)
147 print('Evil deeds done', file=sys.stderr)
148
149
150 #end raw]]></configfile>
151 </configfiles>
152 <inputs/>
153 <outputs>
154 <data name="APIK_mutate_log" format="txt" label="APIK_mutate_log" hidden="false"/>
155 </outputs>
156 <tests>
157 <test>
158 <output name="APIK_mutate_log" value="APIK_mutate_log_sample" compare="sim_size" delta="100"/>
159 </test>
160 </tests>
161 <help><![CDATA[
162
163 **What it Does**
164
165 Regenerates fresh API keys for the ToolFactory administrative user and rewrites them into all the relevant utility code.
166
167 The Docker image is constructed with a new set of keys but they are identical in every image, so running this tool
168 in that image makes sure that while it is running, it is not using the distributed keys. It will do the same thing for a
169 local disk installation but has already been run once at first boot. No real harm running it again to recycle all your keys
170 if you like.
171
172 Will break the ToolFactory if it breaks.
173 Safe in Docker since it's not persistent :)
174 Use at your own peril.
175 This is a crazy tool to run.
176
177 This script consistently fails tool test, but actually works fine on a ToolFactory docker or local installation.
178 It reuses code from the initial configuration to create new, random API keys for the admin logins.
179
180
181 ]]></help>
182 <citations>
183 <citation type="doi">10.1093/bioinformatics/bts573</citation>
184 </citations>
185 </tool>
186