instance-apache-replicate.cfg.in 25.6 KB
Newer Older
1
{% if slap_software_type in software_type %}
2
{% import "caucase" as caucase with context %}
3 4
{#- SERVER_POLLUTED_KEY_LIST is a list of keys which comes from various SlapOS Master implementations, which mix request and publish keys on each slave information -#}
{%- set SERVER_POLLUTED_KEY_LIST = ['connection-parameter-hash', 'timestamp', 'slave_title', 'slap_software_type'] -%}
5
{%- set TRUE_VALUES = ['y', 'yes', '1', 'true'] -%}
6
{%- set GOOD_CIPHER_LIST = ['ECDHE-ECDSA-AES256-GCM-SHA384', 'ECDHE-RSA-AES256-GCM-SHA384', 'ECDHE-ECDSA-AES128-GCM-SHA256', 'ECDHE-RSA-AES128-GCM-SHA256', 'ECDHE-ECDSA-WITH-CHACHA20-POLY1305', 'ECDHE-RSA-WITH-CHACHA20-POLY1305', 'ECDHE-RSA-AES256-CBC-SHA', 'ECDHE-RSA-AES128-CBC-SHA', 'ECDHE-ECDSA-AES256-CBC-SHA', 'ECDHE-ECDSA-AES128-CBC-SHA', 'RSA-AES256-CBC-SHA', 'RSA-AES128-CBC-SHA', 'ECDHE-RSA-3DES-EDE-CBC-SHA', 'RSA-3DES-EDE-CBC-SHA'] %}
7
{% set aikc_enabled = slapparameter_dict.get('automatic-internal-kedifa-caucase-csr', 'true').lower() in TRUE_VALUES %}
8 9 10 11 12 13
[jinja2-template-base]
recipe = slapos.recipe.template:jinja2
rendered = ${buildout:directory}/${:filename}
extra-context =
context =
    import json_module json
14
    raw common_profile {{ common_profile }}
15 16
    ${:extra-context}

17
{% set popen = functools_module.partial(subprocess_module.Popen, stdout=subprocess_module.PIPE, stderr=subprocess_module.STDOUT, stdin=subprocess_module.PIPE) %}
18
{% set part_list = [] %}
19 20
{% set single_type_key = 'single-' %}
{% if slap_software_type == "replicate" %}
21 22 23 24 25 26 27 28
{%   set frontend_type = slapparameter_dict.pop('-frontend-type', 'single-default') %}
{% elif slap_software_type in ['default', 'RootSoftwareInstance'] %}
{%   set frontend_type = "%s%s" % (single_type_key, 'custom-personal') %}
{% else %}
{%   set frontend_type = "%s%s" % (single_type_key, slap_software_type) %}
{% endif %}
{% set frontend_quantity = slapparameter_dict.pop('-frontend-quantity', '1') | int %}
{% set slave_list_name = 'extra_slave_instance_list' %}
29 30 31
{% set frontend_list = [] %}
{% set frontend_section_list = [] %}
{% set request_dict = {} %}
32
{% set namebase = 'caddy-frontend' %}
33
# XXX Dirty hack, not possible to define default value before
34 35 36 37
{% set sla_computer_caddy_1_key = '-sla-1-computer_guid' %}
{% if not sla_computer_caddy_1_key in slapparameter_dict %}
{%   do slapparameter_dict.__setitem__(sla_computer_caddy_1_key, '${slap-connection:computer-id}') %}
{% endif %}
38 39 40 41
{% set sla_computer_kedifa_key = '-sla-kedifa-computer_guid' %}
{% if not sla_computer_kedifa_key in slapparameter_dict %}
{%   do slapparameter_dict.__setitem__(sla_computer_kedifa_key, '${slap-connection:computer-id}') %}
{% endif %}
42

43
# Here we request individually each frontend.
44
# The presence of sla parameters is checked and added if found
45 46 47 48
{% for i in range(1, frontend_quantity + 1) %}
{%   set frontend_name = "%s-%s" % (namebase, i) %}
{%   set request_section_title = 'request-%s' % frontend_name %}
{%   set sla_key = "-sla-%s-" % i %}
49 50 51 52 53 54 55 56 57
{%   set sla_key_length = sla_key | length %}
{%   set sla_dict = {} %}
{%   set config_key = "-frontend-config-%s-" % i %}
{%   set config_key_length = config_key | length %}
{%   set config_dict = {} %}
{%   for key in slapparameter_dict.keys() %}
{%     if key.startswith(sla_key) %}
{%       do sla_dict.__setitem__(key[sla_key_length:], slapparameter_dict.pop(key)) %}
# We check for specific configuration regarding the frontend
58
{%     elif key.startswith(config_key) %}
59
{%       do config_dict.__setitem__(key[config_key_length:], slapparameter_dict.pop(key)) %}
60 61 62 63 64
{%     endif %}
{%   endfor %}
{%   do frontend_list.append(frontend_name) %}
{%   do frontend_section_list.append(request_section_title) %}
{%   do part_list.append(request_section_title) %}
65 66 67 68 69 70 71 72 73
# Filling request dict for slave
{%   set state_key = "-frontend-%s-state" % i %}
{%   do request_dict.__setitem__(request_section_title,
                                  {
                                  'config': config_dict,
                                  'name': frontend_name,
                                  'sla': sla_dict,
                                  'state': slapparameter_dict.pop(state_key, None)
                                  }) %}
74
{% endfor %}
75

76
{% set authorized_slave_string_list = slapparameter_dict.pop('-frontend-authorized-slave-string', '').split() %}
77
{% set authorized_slave_list = [] %}
78
{% set rejected_slave_dict = {} %}
79
{% set rejected_slave_title_dict = {} %}
80
{% set warning_slave_dict = {} %}
81
{% set used_host_list = [] %}
Łukasz Nowak's avatar
Łukasz Nowak committed
82
{% set unauthorized_message = 'slave not authorized' %}
83
{% for slave in sorted(slave_instance_list) %}
84
{%   set slave_error_list = [] %}
85
{%   set slave_warning_list = [] %}
86
{%   set slave_server_alias_unclashed = [] %}
87 88 89
{%   set slave_type = slave.get('type') %}
{%   if slave_type == 'eventsource' %}
{%     do slave_error_list.append('type:eventsource is not implemented') %}
90
{%   elif slave_type not in [None, '', 'default', 'zope', 'redirect', 'notebook', 'websocket'] %}
91 92
{%     do slave_error_list.append('type:%s is not supported' % (slave_type,)) %}
{%   endif %}
93 94 95 96 97 98 99 100 101
{#   Check ciphers #}
{%   set slave_cipher_list = slave.get('ciphers', '').strip().split() %}
{%   if slave_cipher_list %}
{%     for cipher in slave_cipher_list %}
{%       if cipher not in GOOD_CIPHER_LIST  %}
{%         do slave_error_list.append('Cipher %r is not supported.' % (cipher,)) %}
{%       endif %}
{%     endfor %}
{%   endif %}
102
{#   BBB: apache_custom_https AND apache_custom_http #}
103 104
{%   set custom_domain = slave.get('custom_domain') %}
{%   if custom_domain and custom_domain in used_host_list %}
105
{%      do slave_error_list.append('custom_domain %r clashes' % (custom_domain,)) %}
106 107 108 109
{%   else %}
{%      do used_host_list.append(custom_domain) %}
{%   endif %}
{%   if slave.get('server-alias') %}
110
{%     for slave_alias in ('' ~ slave['server-alias']).split() %}
111 112 113 114 115 116
{%       if slave_alias.startswith('*.') %}
{%         set clean_slave_alias = slave_alias[2:] %}
{%       else %}
{%         set clean_slave_alias = slave_alias %}
{%       endif %}
{%       if not validators.domain(clean_slave_alias) %}
117
{%         do slave_error_list.append('server-alias \'%s\' not valid' % (slave_alias,)) %}
118
{%       else %}
119 120 121
{%         if slave_alias in slave_server_alias_unclashed or slave_alias == custom_domain %}
{#           optionally do something about reporting back that server-alias has been unclashed #}
{%         elif slave_alias in used_host_list %}
122
{%           do slave_error_list.append('server-alias \'%s\' clashes' % (slave_alias,)) %}
123
{%         else %}
124
{%           do slave_server_alias_unclashed.append(slave_alias) %}
125 126 127 128
{%           do used_host_list.append(slave_alias) %}
{%         endif %}
{%       endif %}
{%     endfor %}
129
{%     do slave.__setitem__('server-alias', ' '.join(slave_server_alias_unclashed)) %}
130
{%   endif %}
131 132
{%   for key in ['caddy_custom_http', 'caddy_custom_https', 'apache_custom_http', 'apache_custom_https'] %}
{%     if slave.get(key) %}
133
{%       if not slave.get('slave_reference') in authorized_slave_string_list %}
Łukasz Nowak's avatar
Łukasz Nowak committed
134 135
{%         if not unauthorized_message in slave_error_list %}
{%           do slave_error_list.append(unauthorized_message) %}
136
{%         endif %}
137 138
{%       endif %}
{%     endif %}
139
{%   endfor %} {# for key in ['caddy_custom_http', 'caddy_custom_https', 'apache_custom_http', 'apache_custom_https'] #}
140
{%   if slave.get('url') %}
141
{%     if subprocess_module.call([caddy_backend_url_validator, '' ~ slave['url']]) == 1 %}
142
{%       do slave_error_list.append('slave url %r invalid' % (slave['url'],)) %}
143
{%     endif %}
144 145
{%   endif %}
{%   if slave.get('https-url') %}
146
{%     if subprocess_module.call([caddy_backend_url_validator, '' ~ slave['https-url']]) == 1 %}
147
{%       do slave_error_list.append('slave https-url %r invalid' % (slave['https-url'],)) %}
148
{%     endif %}
149
{%   endif %}
150 151 152 153 154 155 156 157
{%   set ssl_proxy_ca_crt = slave.get('ssl_proxy_ca_crt') %}
{%   if ssl_proxy_ca_crt %}
{%     set check_popen = popen([parameter_dict['openssl'], 'x509', '-noout']) %}
{%     do check_popen.communicate(ssl_proxy_ca_crt) %}
{%     if check_popen.returncode != 0 %}
{%       do slave_error_list.append('ssl_proxy_ca_crt is invalid') %}
{%     endif %}
{%   endif %}
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
{# BBB: SlapOS Master non-zero knowledge BEGIN #}
{%   for key in ['ssl_key', 'ssl_crt', 'ssl_ca_crt'] %}
{%     if key in slave %}
{%       do slave_warning_list.append('%s is obsolete, please use key-upload-url' % (key,)) %}
{%     endif %}
{%   endfor %}
{%   if slave.get('ssl_ca_crt') and not (slave.get('ssl_crt') and slave.get('ssl_key')) %}
{%     do slave_error_list.append('ssl_ca_crt is present, so ssl_crt and ssl_key are required')  %}
{%   endif %}
{%   if slave.get('ssl_key') and slave.get('ssl_crt') %}
{%     set key_popen = popen([parameter_dict['openssl'], 'rsa', '-noout', '-modulus']) %}
{%     set crt_popen = popen([parameter_dict['openssl'], 'x509', '-noout', '-modulus']) %}
{%     set key_modulus = key_popen.communicate(slave['ssl_key'])[0] | trim %}
{%     set crt_modulus = crt_popen.communicate(slave['ssl_crt'])[0] | trim %}
{%     if not key_modulus or key_modulus != crt_modulus  %}
{%       do slave_error_list.append('slave ssl_key and ssl_crt does not match')  %}
{%     endif %}
{%   endif %}
{# BBB: SlapOS Master non-zero knowledge END #}
177
{%   if slave.get('custom_domain') %}
178 179 180
{%     set slave_custom_domain = '' ~ slave['custom_domain'] %}
{%     if slave_custom_domain.startswith('*.') %}
{%       set clean_custom_domain = slave_custom_domain[2:] %}
181
{%     else %}
182
{%       set clean_custom_domain = slave_custom_domain %}
183 184
{%     endif %}
{%     if not validators.domain(clean_custom_domain) %}
185
{%       do slave_error_list.append('custom_domain %r invalid' % (slave['custom_domain'],)) %}
186
{%     endif %}
187
{%   endif %}
188
{%   if len(slave_error_list) == 0 %}
189 190 191 192 193 194
{#   Cleanup slave from not needed keys which come from implementation of SlapOS Master #}
{%     set authorized_slave = slave.copy() %}
{%     for key in SERVER_POLLUTED_KEY_LIST %}
{%       do authorized_slave.pop(key, None) %}
{%     endfor %}
{%     do authorized_slave_list.append(authorized_slave) %}
195
{%   else %}
196
{%     do rejected_slave_dict.__setitem__(slave.get('slave_reference'), slave_error_list) %}
197
{%     do rejected_slave_title_dict.__setitem__(slave.get('slave_title'), slave_error_list) %}
198
{%   endif %}
199 200 201
{%   if len(slave_warning_list) > 0 %}
{%     do warning_slave_dict.__setitem__(slave.get('slave_reference'), slave_warning_list) %}
{%   endif %}
202
{% endfor %}
203
{% do authorized_slave_list.sort() %}
204

205 206 207
[monitor-instance-parameter]
monitor-httpd-port = {{ slapparameter_dict.get('monitor-httpd-port', '8196') }}

208 209
[replicate]
<= slap-connection
210
recipe = slapos.cookbook:requestoptional.serialised
211 212 213 214 215 216 217 218 219 220 221
config-monitor-cors-domains = {{ slapparameter_dict.get('monitor-cors-domains', 'monitor.app.officejs.com') }}
config-monitor-username = ${monitor-instance-parameter:username}
config-monitor-password = ${monitor-htpasswd:passwd}

{% set frontend_software_url_key = "-frontend-software-release-url" %}
{% if slapparameter_dict.has_key(frontend_software_url_key) %}
software-url = {{ slapparameter_dict.pop(frontend_software_url_key) }}
{% else %}
software-url = ${slap-connection:software-release-url}
{% endif %}
software-type = {{frontend_type}}
222
return = private-ipv4 public-ipv4 slave-instance-information-list monitor-base-url csr_id-url csr_id-certificate
223 224

{% for section, frontend_request in request_dict.iteritems() %}
225
{%   set state = frontend_request.get('state', '') %}
226 227 228
[{{section}}]
<= replicate
name = {{ frontend_request.get('name') }}
229 230 231
{%   if state %}
state = {{ state }}
{%   endif %}
232 233 234 235
config-slave-kedifa-information = ${request-kedifa:connection-slave-kedifa-information}
config-kedifa-caucase-url = ${request-kedifa:connection-caucase-url}
config-master-key-download-url = ${request-kedifa:connection-master-key-download-url}
config-cluster-identification = {{ cluster_identification }}
236 237 238 239
{#   Do not send additional parameters for destroyed nodes #}
{%   if state != 'destroyed' %}
{%     set slave_configuration_dict = slapparameter_dict %}
{%     do slave_configuration_dict.update(frontend_request.get('config')) %}
240 241
{# sort_keys are important in order to avoid shuffling parameters on each run #}
{%     do slave_configuration_dict.__setitem__(slave_list_name, json_module.dumps(authorized_slave_list, sort_keys=True)) %}
242 243
{%     do slave_configuration_dict.__setitem__("frontend-name", frontend_request.get('name')) %}
{%-     for config_key, config_value in slave_configuration_dict.iteritems() %}
244
config-{{ config_key }} = {{ dumps(config_value) }}
245 246
{%     endfor -%}
{%   endif %}
247
{%   if frontend_request.get('sla') %}
248
{%     for parameter, value in frontend_request.get('sla').iteritems() %}
249
sla-{{ parameter }} = {{ value }}
250 251 252
{%     endfor %}
{%   endif %}
{% endfor %}
253

254 255 256 257 258 259
{%  set warning_list = [] %}
{%  for key in ['apache-certificate', 'apache-key'] %}
{%    if key in slapparameter_dict %}
{%      do warning_list.append('%s is obsolete, please use master-key-upload-url' % (key, )) %}
{%    endif %}
{%  endfor %}
260 261 262 263 264 265 266

[publish-information]
<= monitor-publish
recipe = slapos.cookbook:publish
domain = {{ slapparameter_dict.get('domain') }}
slave-amount = {{ slave_instance_list | length }}
accepted-slave-amount = {{ authorized_slave_list | length }}
267
rejected-slave-amount = {{ rejected_slave_dict | length }}
268 269
{# sort_keys are important in order to avoid shuffling parameters on each run #}
rejected-slave-dict = {{ dumps(json_module.dumps(rejected_slave_title_dict, sort_keys=True)) }}
270
rejected-slave-promise-url = ${rejected-slave-promise:config-url}
271 272
master-key-upload-url = ${request-kedifa:connection-master-key-upload-url}
master-key-generate-auth-url = ${request-kedifa:connection-master-key-generate-auth-url}
273
kedifa-caucase-url = ${request-kedifa:connection-caucase-url}
274
{% if len(warning_list) > 0 %}
275 276
{# sort_keys are important in order to avoid shuffling parameters on each run #}
warning-list = {{ dumps(json_module.dumps(warning_list, sort_keys=True)) }}
277 278
{% endif %}
{% if len(warning_slave_dict) > 0 %}
279 280
{# sort_keys are important in order to avoid shuffling parameters on each run #}
warning-slave-dict = {{ dumps(json_module.dumps(warning_slave_dict, sort_keys=True)) }}
281
{% endif %}
282
{% if not aikc_enabled %}
283 284 285 286 287
kedifa-csr_id-url = ${request-kedifa:connection-csr_id-url}
kedifa-csr_id-certificate = ${request-kedifa:connection-csr_id-certificate}
{% for frontend in frontend_list %}
  {% set section_part = '${request-' + frontend %}
{{ frontend }}-csr_id-url = {{ section_part }}:connection-csr_id-url}
288
{{ frontend }}-csr_id-certificate = {{ section_part }}:connection-csr_id-certificate}
289
{% endfor %}
290
{% endif %}
291 292 293 294 295 296 297 298 299 300 301 302

#----------------------------
#--
#-- Publish slave information
[publish-slave-information]
recipe = slapos.cookbook:softwaretype
default = ${dynamic-publish-slave-information:rendered}
RootSoftwareInstance = ${dynamic-publish-slave-information:rendered}
replicate = ${dynamic-publish-slave-information:rendered}
custom-personal = ${dynamic-publish-slave-information:rendered}
custom-group = ${dynamic-publish-slave-information:rendered}

303 304 305 306 307 308 309 310 311 312 313
[request-kedifa]
<= slap-connection
recipe = slapos.cookbook:requestoptional.serialised
config-monitor-cors-domains = {{ slapparameter_dict.get('monitor-cors-domains', 'monitor.app.officejs.com') }}
config-monitor-username = ${monitor-instance-parameter:username}
config-monitor-password = ${monitor-htpasswd:passwd}
{% for key in ['kedifa_port', 'caucase_port'] -%}
{%-   if key in slapparameter_dict %}
config-{{ key }} = {{ dumps(slapparameter_dict[key]) }}
{%-   endif %}
{%- endfor %}
314
config-slave-list = {{ dumps(authorized_slave_list) }}
315 316 317 318 319 320 321 322 323 324
config-cluster-identification = {{ cluster_identification }}

{% set frontend_software_url_key = "-frontend-software-release-url" %}
{% if slapparameter_dict.has_key(frontend_software_url_key) %}
software-url = {{ slapparameter_dict.pop(frontend_software_url_key) }}
{% else %}
software-url = ${slap-connection:software-release-url}
{% endif %}
software-type = kedifa
name = kedifa
325
return = slave-kedifa-information master-key-generate-auth-url master-key-upload-url master-key-download-url caucase-url csr_id-url csr_id-certificate  monitor-base-url
326 327 328 329 330 331 332 333
{% set sla_kedifa_key = "-sla-kedifa-" %}
{% set sla_kedifa_key_length = sla_kedifa_key | length %}
{% for key in slapparameter_dict.keys() %}
{%   if key.startswith(sla_kedifa_key) %}
sla-{{ key[sla_kedifa_key_length:] }} = {{ slapparameter_dict.pop(key) }}
{%   endif %}
{% endfor %}

334 335
[rejected-slave-information]
{% for slave_id, rejected_list in rejected_slave_dict.iteritems() %}
336 337
{# sort_keys are important in order to avoid shuffling parameters on each run #}
{{ slave_id }} = {{ dumps(json_module.dumps(rejected_list, sort_keys=True)) }}
338 339
{% endfor %}

340 341
[warning-slave-information]
{% for slave_id, warning_list in warning_slave_dict.iteritems() %}
342 343
{# sort_keys are important in order to avoid shuffling parameters on each run #}
{{ slave_id }} = {{ dumps(json_module.dumps(warning_list, sort_keys=True)) }}
344 345
{% endfor %}

346
[slave-information]
347
{% for frontend_section in frontend_section_list %}
348
{{ frontend_section }} = {{ "${%s:connection-slave-instance-information-list}" % frontend_section }}
349
{% endfor %}
350

351
[active-slave-instance]
352
{% set active_slave_instance_list = [] %}
353 354 355
{% for slave_instance in slave_instance_list %}
{# Provide a list of slave titles send by master, in order to filter out already destroyed slaves #}
{# Note: This functionality is not yet covered by tests, please modify with care #}
356
{%   do active_slave_instance_list.append(slave_instance['slave_reference']) %}
357
{% endfor %}
358 359
{# sort_keys are important in order to avoid shuffling parameters on each run #}
active-slave-instance-list = {{ json_module.dumps(active_slave_instance_list, sort_keys=True) }}
360

361 362 363 364 365 366 367
[dynamic-publish-slave-information]
< = jinja2-template-base
template = {{ template_publish_slave_information }}
filename = dynamic-publish-slave-information.cfg
extensions = jinja2.ext.do
extra-context =
    section slave_information slave-information
368
    section rejected_slave_information rejected-slave-information
369
    section active_slave_instance_dict active-slave-instance
370
    section warning_slave_information warning-slave-information
371
    key slave_kedifa_information request-kedifa:connection-slave-kedifa-information
372 373 374

[monitor-conf-parameters]
monitor-url-list +=
375
  ${request-kedifa:connection-monitor-base-url}
376 377
{% for frontend in frontend_section_list %}
{{ '  ${' + frontend + ':connection-monitor-base-url}' }}
378
{% endfor %}
379

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
{% if aikc_enabled %}
[directory]
recipe = slapos.cookbook:mkdirectory

bin = ${buildout:directory}/bin/
srv = ${buildout:directory}/srv/
aikc = ${:srv}/aikc

[aikc-config]
caucase-url = ${request-kedifa:connection-caucase-url}

csr = ${directory:aikc}/csr.pem
key = ${directory:aikc}/key.pem
ca-certificate = ${directory:aikc}/cas-ca-certificate.pem
crl = ${directory:aikc}/crl.pem
user-ca-certificate = ${directory:aikc}/user-ca-certificate.pem
user-crl = ${directory:aikc}/user-crl.pem
user-created = ${directory:aikc}/user-created
csr_id = ${directory:aikc}/csr_id

[aikc-user-csr]
recipe = plone.recipe.command
organization = {{ cluster_identification }}
organizational_unit = Automatic Internal Kedifa Caucase CSR
command =
  if [ ! -f ${:csr} ] && [ ! -f ${:key} ]  ; then
    {{ parameter_dict['openssl'] }} req -new -sha256 \
      -newkey rsa:2048 -nodes -keyout ${:key} \
      -subj "/O=${:organization}/OU=${:organizational_unit}" \
      -out ${:csr}
  fi
update-command = ${:command}
csr = ${aikc-config:csr}
key = ${aikc-config:key}
stop-on-error = True


[aikc-caucase-wrapper]
{# jinja2 instead of wrapper is used with context to remove py'u' #}
recipe = slapos.recipe.template:jinja2
context =
  key caucase_url aikc-config:caucase-url
template = inline:#!{{ parameter_dict['dash'] }}/bin/dash
  exec {{ parameter_dict['bin_directory'] }}/caucase \
{# raw block to use context #}
{% raw %}
  --ca-url {{ caucase_url }} \
{% endraw %}
  --ca-crt ${aikc-config:ca-certificate} \
  --user-ca-crt ${aikc-config:user-ca-certificate} \
  --user-crl ${aikc-config:user-crl} \
  --crl ${aikc-config:crl} \
  "$@"

rendered = ${directory:bin}/aikc-caucase-wrapper
mode = 0700

{% do part_list.append('aikc-create-user') %}
[aikc-create-user]
recipe = plone.recipe.command
stop-on-error = True
update-command = ${:command}
command =
  if ! [ -f ${aikc-config:user-created} ] ; then
    ${aikc-caucase-wrapper:rendered} --mode user --send-csr ${aikc-user-csr:csr} > ${aikc-config:csr_id} || exit 1
    cut -d ' ' -f 1 ${aikc-config:csr_id} || exit 1
    csr_id=`cut -d ' ' -f 1 ${aikc-config:csr_id}`
    sleep 1
    ${aikc-caucase-wrapper:rendered} --mode user --get-crt $csr_id ${aikc-config:key} || exit 1
    touch ${aikc-config:user-created}
  fi

452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
{% do part_list.append('aikc-user-caucase-updater') %}
{{ caucase.updater(
     prefix='aikc-user-caucase-updater',
     buildout_bin_directory=parameter_dict['bin_directory'],
     updater_path='${directory:service}/aikc-user-caucase-updater',
     url='${aikc-config:caucase-url}',
     data_dir='${directory:srv}/caucase-updater',
     crt_path='${aikc-config:key}',
     ca_path='${aikc-config:user-ca-certificate}',
     crl_path='${aikc-config:user-crl}',
     key_path='${aikc-config:key}',
     mode='user',
)}}


467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
[aikc-check-certificate]
recipe = slapos.recipe.template:jinja2
rendered = ${directory:bin}/aikc-check-certificate
template = inline:
  import sys
  import ssl
  import urlparse
  certificate = sys.argv[2]
  parsed = urlparse.urlparse(sys.argv[1])
  got_certificate = ssl.get_server_certificate((parsed.hostname, parsed.port))
  sys.exit(0) if certificate.strip() == got_certificate.strip() else sys.exit(1)

{% for csr in frontend_list + ['kedifa'] %}
[aikc-{{ csr }}-wrapper]
{# jinja2 instead of wrapper is used with context to remove py'u' #}
recipe = slapos.recipe.template:jinja2
context =
  key csr_id_url request-{{ csr }}:connection-csr_id-url
  key csr_id_certificate request-{{ csr }}:connection-csr_id-certificate
template = inline:#!{{ parameter_dict['dash'] }}/bin/dash
  test -f ${directory:aikc}/{{ csr }}-done && exit 0
  ${buildout:executable} ${aikc-check-certificate:rendered} \
{# raw block to use context #}
{% raw %}
  {{ csr_id_url }} \
  """{{ csr_id_certificate }}"""
{% endraw %}
  if [ $? = 0 ]; then
    csr_id=`{{ parameter_dict['curl'] }}/bin/curl -s -k -g \
{% raw %}
  {{ csr_id_url }} \
{% endraw %}
  ` || exit 1
    ${aikc-caucase-wrapper:rendered} --user-key ${aikc-config:key} --sign-csr $csr_id && touch ${directory:aikc}/{{ csr }}-done
  fi
rendered = ${directory:bin}/aikc-{{ csr }}-wrapper
mode = 0700

{% do part_list.append('aikc-%s' % (csr,)) %}
[aikc-{{ csr }}]
recipe = plone.recipe.command
stop-on-error = True
command =
  ${aikc-{{ csr }}-wrapper:rendered}
update-command = ${:command}
{% endfor %}
{% endif %}

515 516 517 518 519 520 521
[rejected-slave-json]
recipe = slapos.recipe.template:jinja2
filename = rejected-slave.json
directory = ${directory:promise-output}
rendered = ${:directory}/${:filename}
template = {{ parameter_dict['template_empty'] }}
{% if rejected_slave_title_dict %}
522 523
{# sort_keys are important in order to avoid shuffling parameters on each run #}
content = {{ dumps(json_module.dumps(rejected_slave_title_dict, indent=2, sort_keys=True)) }}
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
{% else %}
content =
{% endif %}
context =
  key content :content

[directory]
service = ${:etc}/service
promise-output = ${:srv}/promise-output

[rejected-slave-publish-configuration]
ip = {{ instance_parameter['ipv6-random'] }}
port = 14455

[rejected-slave-publish]
directory = ${rejected-slave-json:directory}
url = https://${rejected-slave-password:user}:${rejected-slave-password:passwd}@[${rejected-slave-publish-configuration:ip}]:${rejected-slave-publish-configuration:port}/${rejected-slave-json:filename}
recipe = slapos.cookbook:wrapper
command-line = {{ parameter_dict['caddy'] }}
  -conf ${rejected-slave-template:rendered}
  -log stderr
  -http2=true
  -disable-http-challenge
  -disable-tls-alpn-challenge
  -root ${:directory}

wrapper-path = ${directory:service}/rejected-slave-publish
551
hash-existing-files =
552
  ${buildout:directory}/software_release/buildout.cfg
553
hash-files =
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
  ${rejected-slave-template:rendered}
  ${rejected-slave-certificate:certificate}

[rejected-slave-certificate]
recipe = plone.recipe.command
certificate = ${directory:etc}/rejected-slave.pem
key = ${:certificate}

stop-on-error = True
update-command = ${:command}
command =
  [ -f ${:certificate} ] && find ${:certificate} -type f -mtime +3 -delete
  if ! [ -f ${:certificate} ] ; then
    openssl req -new -newkey rsa:2048 -sha256 -subj \
      "/CN=${rejected-slave-publish-configuration:ip}" \
      -days 5 -nodes -x509 -keyout ${:certificate} -out ${:certificate}
  fi

[rejected-slave-password]
recipe = slapos.cookbook:generate.password
storage-path = ${directory:etc}/.rejected-slave.passwd
bytes = 8
user = admin

[rejected-slave-template]
recipe = slapos.recipe.template:jinja2
template = inline:
  https://:${rejected-slave-publish-configuration:port}/ {
    basicauth / ${rejected-slave-password:user} ${rejected-slave-password:passwd}
    tls ${rejected-slave-certificate:certificate} ${rejected-slave-certificate:key}
    bind ${rejected-slave-publish-configuration:ip}
    log stderr
    errors stderr
  }

rendered = ${directory:etc}/Caddyfile-rejected-slave

[promise-rejected-slave-publish-ip-port]
592
<= monitor-promise-base
593 594 595 596 597 598
module = check_port_listening
name = rejected-slave-publish-ip-port-listening.py
config-hostname = ${rejected-slave-publish-configuration:ip}
config-port = ${rejected-slave-publish-configuration:port}

[rejected-slave-promise]
599
<= monitor-promise-base
600 601 602 603 604 605 606
module = check_port_listening
module = check_file_state
name = rejected-slave.py
config-filename = ${rejected-slave-json:rendered}
config-state = empty
config-url = ${rejected-slave-publish:url}

607
[buildout]
608 609 610
extends =
  {{ common_profile }}
  {{ template_monitor }}
611 612 613 614
parts =
  monitor-base
  publish-slave-information
  publish-information
615
  request-kedifa
616 617
  rejected-slave-promise
  promise-rejected-slave-publish-ip-port
618
{% for part in part_list %}
619
{{ '  %s' % part }}
620
{% endfor %}
621
#      publish-information
622
{% endif %}