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