fix(general): Remove grains.get_or_set_hash use

since it was removed in v3005
This commit is contained in:
jeanluc
2023-01-09 23:56:59 +01:00
parent 1e4b155da6
commit 58f8ee8c29

View File

@@ -3,13 +3,13 @@
{#- If latest was specified, query the official download server for the latest version. -#} {#- If latest was specified, query the official download server for the latest version. -#}
{%- if 'latest' == mapdata.version -%} {%- if "latest" == mapdata.version -%}
{%- set latest = salt['http.query']( {%- set latest = salt["http.query"](
mapdata.lookup.pkg.latest, mapdata.lookup.pkg.latest,
decode=True, decode_type='json' decode=True, decode_type="json"
)['dict']['latest']['version'] )["dict"]["latest"]["version"]
-%} -%}
{%- do mapdata.update({'version': latest}) -%} {%- do mapdata.update({"version": latest}) -%}
{%- endif -%} {%- endif -%}
@@ -25,15 +25,15 @@
{%- do mapdata.update({"_rw": []}) -%} {%- do mapdata.update({"_rw": []}) -%}
{%- for path in [ {%- for path in [
('app_data_path', 'server', mapdata.lookup.paths.data, mapdata.lookup.paths.work | path_join('data')), ("app_data_path", "server", mapdata.lookup.paths.data, mapdata.lookup.paths.work | path_join("data")),
('root_path', 'log', mapdata.lookup.paths.log, mapdata.lookup.paths.work | path_join('log')), ("root_path", "log", mapdata.lookup.paths.log, mapdata.lookup.paths.work | path_join("log")),
('root', 'repository', mapdata.lookup.paths.repos, mapdata.lookup.paths.data | path_join('gitea-repositories')), ("root", "repository", mapdata.lookup.paths.repos, mapdata.lookup.paths.data | path_join("gitea-repositories")),
] -%} ] -%}
{%- set var, topic, current, default = path -%} {%- set var, topic, current, default = path -%}
{%- if current != default -%} {%- if current != default -%}
{%- do mapdata | update_dict_key_value('config:' ~ topic, {var: current}) -%} {%- do mapdata | update_dict_key_value("config:" ~ topic, {var: current}) -%}
{#- log directory is handled in service template -#} {#- log directory is handled in service template -#}
{%- if topic != 'log' -%} {%- if topic != "log" -%}
{%- do mapdata._rw.append(current) -%} {%- do mapdata._rw.append(current) -%}
{%- endif -%} {%- endif -%}
{%- endif -%} {%- endif -%}
@@ -41,44 +41,54 @@
{#- {#-
If the secrets were not provided, get them from a grain/generate and save them there. If the secrets were not provided, generate them here. Since grains.get_or_set_hash was
This avoids removed, there is no easy way to cache the random value to avoid changing it over and over.
It cannot be read from `app.ini` since the configparser serializer throws an exception.
Generating it here avoids
a) calling the gitea binary in the app.ini template (`gitea generate secret SECRET_KEY`) a) calling the gitea binary in the app.ini template (`gitea generate secret SECRET_KEY`)
b) and special-casing to avoid regenerating those secrets every run. b) and special-casing to avoid regenerating those secrets every run.
The INTERNAL_TOKEN has a special format, but it can be configured to be read from a separate file. The INTERNAL_TOKEN has a special format, but it can be configured to be read from a separate file.
"." is used in place of ":" to avoid libmapstack automap. @TODO sensible to indulge in that magic?
-#} -#}
{%- set current_cached = {} -%}
{%- set cache_updated = false -%}
{%- set cache_file = mapdata.lookup.paths.conf | path_join(".saltcache.yml") -%}
{%- if salt["file.file_exists"](cache_file) -%}
{%- set current_cached = salt["jinja.import_yaml"](cache_file) -%}
{%- else -%}
{%- do salt["file.mkdir"](mapdata.lookup.paths.conf) -%}
{%- endif -%}
{%- for var, val in current_cached.items() -%}
{%- do mapdata.config | set_dict_key_value(var, val) -%}
{%- endfor -%}
{%- if not mapdata.config | traverse("security:secret_key", "") -%} {%- if not mapdata.config | traverse("security:secret_key", "") -%}
{%- set secret_key = salt['grains.get_or_set_hash']( {%- do current_cached.update({"security:secret_key": salt["random.get_str"](64, punctuation=false)}) -%}
'gitea.config.security.secret_key', {%- set cache_updated = true -%}
length=64,
chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
)
-%}
{%- do mapdata.config | update_dict_key_value("security", {"secret_key": secret_key}) -%}
{%- endif -%} {%- endif -%}
{%- for jwt in ['oauth2:jwt_secret', 'server:lfs_jwt_secret'] -%} {%- for jwt in ["oauth2:jwt_secret", "server:lfs_jwt_secret"] -%}
{%- if not mapdata.config | traverse(jwt, "") -%} {%- if not mapdata.config | traverse(jwt, "") -%}
{%- set secret = salt['grains.get_or_set_hash']( {%- do current_cached.update({jwt: salt["random.get_str"](43, chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-")}) -%}
'gitea.config.' ~ jwt | replace(":", "."), {%- set cache_updated = true -%}
length=43,
chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-',
)
-%}
{%- do mapdata.config | update_dict_key_value(jwt.split(":")[0], {jwt.split(":")[1]: secret}) -%}
{%- endif -%} {%- endif -%}
{%- endfor -%} {%- endfor -%}
{%- if cache_updated -%}
{%- for var, val in current_cached.items() -%}
{%- do mapdata.config | set_dict_key_value(var, val) -%}
{%- endfor -%}
{%- do salt["file.write"](cache_file, salt["slsutil.serializer"]("yaml", current_cached)) -%}
{%- do salt["file.set_mode"]("0600") -%}
{%- endif -%}
{%- do mapdata.update({"_generate_token": false}) -%} {%- do mapdata.update({"_generate_token": false}) -%}
{%- if not mapdata.config | traverse("security:internal_token") -%} {%- if not mapdata.config | traverse("security:internal_token") -%}
{%- do mapdata.config | update_dict_key_value("security", {"internal_token_uri": mapdata.lookup.paths.internal_token_uri}) -%} {%- do mapdata.config | update_dict_key_value("security", {"internal_token_uri": mapdata.lookup.paths.internal_token_uri}) -%}
{%- if 'file://' == mapdata.lookup.paths.internal_token_uri[:7] -%} {%- if "file://" == mapdata.lookup.paths.internal_token_uri[:7] -%}
{%- do mapdata.update({"_generate_token": true}) -%} {%- do mapdata.update({"_generate_token": true}) -%}
{%- endif %} {%- endif -%}
{%- endif -%} {%- endif -%}
{%- do mapdata.config | update_dict_key_value("security", {"install_lock": true}) -%} {%- do mapdata.config | update_dict_key_value("security", {"install_lock": true}) -%}