instance-apache-replicate.cfg.in 34.8 KB
Newer Older
1
{% if instance_parameter_dict['slap-software-type'] in software_type %}
2
{% set aibcc_enabled = True %}
3
{% import "caucase" as caucase with context %}
4 5
{#- 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'] -%}
6
{%- set TRUE_VALUES = ['y', 'yes', '1', 'true'] -%}
7
{%- 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'] %}
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
{#- Allow to pass only some parameters to frontend nodes #}
{%- set FRONTEND_NODE_PASSED_KEY_LIST = [
        'plain_http_port',
        'port',
        'apache-certificate',
        'apache-key',
        'domain',
        'enable-http2-by-default',
        'global-disable-http2',
        'mpm-graceful-shutdown-timeout',
        'public-ipv4',
        're6st-verification-url',
        'backend-connect-timeout',
        'backend-connect-retries',
        'ciphers',
        'request-timeout',
        'authenticate-to-backend',
    ]
%}
27
{% set aikc_enabled = slapparameter_dict.get('automatic-internal-kedifa-caucase-csr', 'true').lower() in TRUE_VALUES %}
28
{% set aibcc_enabled = slapparameter_dict.get('automatic-internal-backend-client-caucase-csr', 'true').lower() in TRUE_VALUES %}
29 30 31 32
{# Ports 8401, 8402 and 8410+1..N are reserved for monitor ports on various partitions #}
{% set master_partition_monitor_monitor_httpd_port = 8401 %}
{% set kedifa_partition_monitor_httpd_port = 8402 %}
{% set frontend_monitor_httpd_base_port = 8410 %}
33 34
{% set caucase_host = '[' ~ instance_parameter_dict['ipv6-random'] ~ ']' %}
{% set caucase_netloc = caucase_host ~ ':' ~ instance_parameter_dict['configuration.caucase_backend_client_port'] %}
35
{% set caucase_url = 'http://' ~ caucase_netloc %}
36 37 38 39 40 41
[jinja2-template-base]
recipe = slapos.recipe.template:jinja2
rendered = ${buildout:directory}/${:filename}
extra-context =
context =
    import json_module json
42
    raw profile_common {{ software_parameter_dict['profile_common'] }}
43 44
    ${:extra-context}

45
{% set popen = functools_module.partial(subprocess_module.Popen, stdout=subprocess_module.PIPE, stderr=subprocess_module.STDOUT, stdin=subprocess_module.PIPE) %}
46
{% set part_list = [] %}
47
{% set single_type_key = 'single-' %}
48
{% if instance_parameter_dict['slap-software-type'] == "replicate" %}
49
{%   set frontend_type = slapparameter_dict.pop('-frontend-type', 'single-default') %}
50
{% elif instance_parameter_dict['slap-software-type'] in ['default', 'RootSoftwareInstance'] %}
51 52
{%   set frontend_type = "%s%s" % (single_type_key, 'custom-personal') %}
{% else %}
53
{%   set frontend_type = "%s%s" % (single_type_key, instance_parameter_dict['slap-software-type']) %}
54 55 56
{% endif %}
{% set frontend_quantity = slapparameter_dict.pop('-frontend-quantity', '1') | int %}
{% set slave_list_name = 'extra_slave_instance_list' %}
57 58 59
{% set frontend_list = [] %}
{% set frontend_section_list = [] %}
{% set request_dict = {} %}
60
{% set namebase = 'caddy-frontend' %}
61
# XXX Dirty hack, not possible to define default value before
62 63 64 65
{% 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 %}
66 67 68 69
{% 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 %}
70

71
# Here we request individually each frontend.
72
# The presence of sla parameters is checked and added if found
73 74 75 76
{% 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 %}
77 78 79 80 81 82 83 84 85
{%   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
86
{%     elif key.startswith(config_key) %}
87
{%       do config_dict.__setitem__(key[config_key_length:], slapparameter_dict.pop(key)) %}
88 89
{%     endif %}
{%   endfor %}
90
{%   do config_dict.__setitem__('monitor-httpd-port', frontend_monitor_httpd_base_port + i) %}
91
{%   do config_dict.__setitem__('backend-client-caucase-url', caucase_url) %}
92 93 94 95 96 97
{%   set state_key = "-frontend-%s-state" % i %}
{%   set frontend_state = slapparameter_dict.pop(state_key, None) %}
{%   if frontend_state != 'destroyed' %}
{%     do frontend_list.append(frontend_name) %}
{%     do frontend_section_list.append(request_section_title) %}
{%   endif %}
98
{%   do part_list.append(request_section_title) %}
99
# Filling request dict for slave
100
{%   set request_content_dict = {
101 102 103
                                  'config': config_dict,
                                  'name': frontend_name,
                                  'sla': sla_dict,
104
                                  'state': frontend_state
105 106 107 108
                                  } %}
{%   set frontend_software_url_key = "-frontend-%s-software-release-url" % i %}
{%   do request_content_dict.__setitem__('software-url', slapparameter_dict.get(frontend_software_url_key) or '${slap-connection:software-release-url}') %}
{%   do request_dict.__setitem__(request_section_title, request_content_dict) %}
109
{% endfor %}
110

111
{% set authorized_slave_string_list = [] %}
112
{% set authorized_slave_list = [] %}
113
{% set rejected_slave_dict = {} %}
114
{% set rejected_slave_title_dict = {} %}
115
{% set warning_slave_dict = {} %}
116
{% set used_host_list = [] %}
117
{% for slave in sorted(instance_parameter_dict['slave-instance-list']) %}
118
{%   set slave_error_list = [] %}
119
{%   set slave_warning_list = [] %}
120
{%   set slave_server_alias_unclashed = [] %}
121 122 123
{%   set slave_type = slave.get('type') %}
{%   if slave_type == 'eventsource' %}
{%     do slave_error_list.append('type:eventsource is not implemented') %}
124
{%   elif slave_type not in [None, '', 'default', 'zope', 'redirect', 'notebook', 'websocket'] %}
125 126
{%     do slave_error_list.append('type:%s is not supported' % (slave_type,)) %}
{%   endif %}
127 128 129 130 131 132 133 134 135
{#   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 %}
136 137
{%   set custom_domain = slave.get('custom_domain') %}
{%   if custom_domain and custom_domain in used_host_list %}
138
{%      do slave_error_list.append('custom_domain %r clashes' % (custom_domain,)) %}
139 140 141 142
{%   else %}
{%      do used_host_list.append(custom_domain) %}
{%   endif %}
{%   if slave.get('server-alias') %}
143
{%     for slave_alias in ('' ~ slave['server-alias']).split() %}
144 145 146 147 148 149
{%       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) %}
150
{%         do slave_error_list.append('server-alias \'%s\' not valid' % (slave_alias,)) %}
151
{%       else %}
152 153 154
{%         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 %}
155
{%           do slave_error_list.append('server-alias \'%s\' clashes' % (slave_alias,)) %}
156
{%         else %}
157
{%           do slave_server_alias_unclashed.append(slave_alias) %}
158 159 160 161
{%           do used_host_list.append(slave_alias) %}
{%         endif %}
{%       endif %}
{%     endfor %}
162
{%     do slave.__setitem__('server-alias', ' '.join(slave_server_alias_unclashed)) %}
163
{%   endif %}
164
{%   for url_key in ['url', 'https-url'] %}
165
{%     if url_key in slave %}
166
{%       set url = (slave[url_key] or '').strip() %}
167
{%       if subprocess_module.call([software_parameter_dict['caddy_backend_url_validator'], url]) == 1 or not validators.url(url) %}
168
{%         do slave_error_list.append('slave %s %r invalid' % (url_key, url)) %}
169 170
{%       elif url != slave[url_key] %}
{%         do slave_warning_list.append('slave %s %r has been converted to %r' % (url_key, slave[url_key], url)) %}
171
{%       endif %}
172
{%     endif %}
173
{%   endfor %}
174 175
{%   if 'ssl_proxy_ca_crt' in slave %}
{%     set ssl_proxy_ca_crt = slave.get('ssl_proxy_ca_crt', '') %}
176
{%     set check_popen = popen([software_parameter_dict['openssl'], 'x509', '-noout']) %}
177 178 179 180 181
{%     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 %}
182 183 184 185 186 187 188 189 190 191
{# 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') %}
192 193
{%     set key_popen = popen([software_parameter_dict['openssl'], 'rsa', '-noout', '-modulus']) %}
{%     set crt_popen = popen([software_parameter_dict['openssl'], 'x509', '-noout', '-modulus']) %}
194 195 196 197 198 199 200
{%     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 #}
201
{%   if slave.get('custom_domain') %}
202 203 204
{%     set slave_custom_domain = '' ~ slave['custom_domain'] %}
{%     if slave_custom_domain.startswith('*.') %}
{%       set clean_custom_domain = slave_custom_domain[2:] %}
205
{%     else %}
206
{%       set clean_custom_domain = slave_custom_domain %}
207 208
{%     endif %}
{%     if not validators.domain(clean_custom_domain) %}
209
{%       do slave_error_list.append('custom_domain %r invalid' % (slave['custom_domain'],)) %}
210
{%     endif %}
211
{%   endif %}
212
{%   if len(slave_error_list) == 0 %}
213 214 215 216 217 218
{#   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) %}
219
{%   else %}
220
{%     do rejected_slave_dict.__setitem__(slave.get('slave_reference'), slave_error_list) %}
221
{%     do rejected_slave_title_dict.__setitem__(slave.get('slave_title'), slave_error_list) %}
222
{%   endif %}
223 224 225
{%   if len(slave_warning_list) > 0 %}
{%     do warning_slave_dict.__setitem__(slave.get('slave_reference'), slave_warning_list) %}
{%   endif %}
226
{% endfor %}
227
{% do authorized_slave_list.sort() %}
228

229
[monitor-instance-parameter]
230
monitor-httpd-port = {{ master_partition_monitor_monitor_httpd_port }}
231

232 233
[replicate]
<= slap-connection
234
recipe = slapos.cookbook:requestoptional.serialised
235 236 237 238 239
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}

software-type = {{frontend_type}}
240
return = private-ipv4 public-ipv4 slave-instance-information-list monitor-base-url backend-client-csr_id-url csr_id-url csr_id-certificate backend-haproxy-statistic-url
241

242 243 244 245 246 247 248
{#- Send only needed parameters to frontend nodes #}
{%- set base_node_configuration_dict = {} %}
{%- for key in FRONTEND_NODE_PASSED_KEY_LIST %}
{%-   if key in slapparameter_dict %}
{%-     do base_node_configuration_dict.__setitem__(key,  slapparameter_dict[key]) %}
{%-   endif %}
{%- endfor %}
249
{% for section, frontend_request in request_dict.iteritems() %}
250
{%   set state = frontend_request.get('state', '') %}
251 252 253
[{{section}}]
<= replicate
name = {{ frontend_request.get('name') }}
254
software-url = {{ frontend_request['software-url'] }}
255 256 257
{%   if state %}
state = {{ state }}
{%   endif %}
258 259
config-slave-kedifa-information = ${request-kedifa:connection-slave-kedifa-information}
config-kedifa-caucase-url = ${request-kedifa:connection-caucase-url}
260
config-backend-client-caucase-url = {{ caucase_url }}
261
config-master-key-download-url = ${request-kedifa:connection-master-key-download-url}
262
config-cluster-identification = {{ instance_parameter_dict['root-instance-title'] }}
263 264
{#   Do not send additional parameters for destroyed nodes #}
{%   if state != 'destroyed' %}
265 266
{%     set node_configuration_dict = {} %}
{%     do node_configuration_dict.update(frontend_request.get('config')) %}
267
{# sort_keys are important in order to avoid shuffling parameters on each run #}
268 269 270 271 272 273
{%     do node_configuration_dict.__setitem__(slave_list_name, json_module.dumps(authorized_slave_list, sort_keys=True)) %}
{%     do node_configuration_dict.__setitem__("frontend-name", frontend_request.get('name')) %}
{%-     for config_key, config_value in node_configuration_dict.iteritems() %}
config-{{ config_key }} = {{ dumps(config_value) }}
{%     endfor -%}
{%-     for config_key, config_value in base_node_configuration_dict.iteritems() %}
274
config-{{ config_key }} = {{ dumps(config_value) }}
275 276
{%     endfor -%}
{%   endif %}
277
{%   if frontend_request.get('sla') %}
278
{%     for parameter, value in frontend_request.get('sla').iteritems() %}
279
sla-{{ parameter }} = {{ value }}
280 281 282
{%     endfor %}
{%   endif %}
{% endfor %}
283

284 285 286 287 288 289
{%  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 %}
290 291 292 293 294

[publish-information]
<= monitor-publish
recipe = slapos.cookbook:publish
domain = {{ slapparameter_dict.get('domain') }}
295
slave-amount = {{ instance_parameter_dict['slave-instance-list'] | length }}
296
accepted-slave-amount = {{ authorized_slave_list | length }}
297
rejected-slave-amount = {{ rejected_slave_dict | length }}
298
backend-client-caucase-url = {{ caucase_url }}
299 300
{# 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)) }}
301
rejected-slave-promise-url = ${rejected-slave-promise:config-url}
302 303
master-key-upload-url = ${request-kedifa:connection-master-key-upload-url}
master-key-generate-auth-url = ${request-kedifa:connection-master-key-generate-auth-url}
304
kedifa-caucase-url = ${request-kedifa:connection-caucase-url}
305
{% if len(warning_list) > 0 %}
306 307
{# sort_keys are important in order to avoid shuffling parameters on each run #}
warning-list = {{ dumps(json_module.dumps(warning_list, sort_keys=True)) }}
308 309
{% endif %}
{% if len(warning_slave_dict) > 0 %}
310 311
{# 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)) }}
312
{% endif %}
313 314 315 316 317 318
{% if not aikc_enabled or not aibcc_enabled %}
{% for frontend in frontend_list %}
  {% set section_part = '${request-' + frontend %}
{{ frontend }}-csr_id-certificate = {{ section_part }}:connection-csr_id-certificate}
{% endfor %}
{% endif %}
319
{% if not aikc_enabled %}
320 321 322 323 324
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}
325 326
{% endfor %}
{% endif %}
327 328 329 330
{% for frontend in frontend_list %}
  {% set section_part = '${request-' + frontend %}
{{ frontend }}-backend-haproxy-statistic-url = {{ section_part }}:connection-backend-haproxy-statistic-url}
{% endfor %}
331 332 333 334
{% if not aibcc_enabled %}
{% for frontend in frontend_list %}
  {% set section_part = '${request-' + frontend %}
{{ frontend }}-backend-client-csr_id-url = {{ section_part }}:connection-backend-client-csr_id-url}
335
{% endfor %}
336
{% endif %}
337 338 339 340 341 342 343 344 345 346 347 348

#----------------------------
#--
#-- 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}

349 350 351 352 353 354
[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}
355
config-monitor-httpd-port = {{ kedifa_partition_monitor_httpd_port }}
356 357 358 359 360
{% for key in ['kedifa_port', 'caucase_port'] -%}
{%-   if key in slapparameter_dict %}
config-{{ key }} = {{ dumps(slapparameter_dict[key]) }}
{%-   endif %}
{%- endfor %}
361
config-slave-list = {{ dumps(authorized_slave_list) }}
362
config-cluster-identification = {{ instance_parameter_dict['root-instance-title'] }}
363

364 365 366
{% set software_url_key = "-kedifa-software-release-url" %}
{% if slapparameter_dict.has_key(software_url_key) %}
software-url = {{ slapparameter_dict.pop(software_url_key) }}
367 368 369 370 371
{% else %}
software-url = ${slap-connection:software-release-url}
{% endif %}
software-type = kedifa
name = kedifa
372
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
373 374 375 376 377 378 379 380
{% 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 %}

381 382
[rejected-slave-information]
{% for slave_id, rejected_list in rejected_slave_dict.iteritems() %}
383 384
{# sort_keys are important in order to avoid shuffling parameters on each run #}
{{ slave_id }} = {{ dumps(json_module.dumps(rejected_list, sort_keys=True)) }}
385 386
{% endfor %}

387 388
[warning-slave-information]
{% for slave_id, warning_list in warning_slave_dict.iteritems() %}
389 390
{# sort_keys are important in order to avoid shuffling parameters on each run #}
{{ slave_id }} = {{ dumps(json_module.dumps(warning_list, sort_keys=True)) }}
391 392
{% endfor %}

393
[slave-information]
394
{% for frontend_section in frontend_section_list %}
395
{{ frontend_section }} = {{ "${%s:connection-slave-instance-information-list}" % frontend_section }}
396
{% endfor %}
397

398
[active-slave-instance]
399
{% set active_slave_instance_list = [] %}
400
{% for slave_instance in instance_parameter_dict['slave-instance-list'] %}
401 402
{# 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 #}
403
{%   do active_slave_instance_list.append(slave_instance['slave_reference']) %}
404
{% endfor %}
405 406
{# 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) }}
407

408 409
[dynamic-publish-slave-information]
< = jinja2-template-base
410
template = {{ software_parameter_dict['profile_replicate_publish_slave_information'] }}
411 412 413 414
filename = dynamic-publish-slave-information.cfg
extensions = jinja2.ext.do
extra-context =
    section slave_information slave-information
415
    section rejected_slave_information rejected-slave-information
416
    section active_slave_instance_dict active-slave-instance
417
    section warning_slave_information warning-slave-information
418
    key slave_kedifa_information request-kedifa:connection-slave-kedifa-information
419

420 421
[monitor-base-url-dict]
kedifa = ${request-kedifa:connection-monitor-base-url}
422
{% for frontend in frontend_section_list %}
423
{{ frontend }} = {{ '${' + frontend + ':connection-monitor-base-url}' }}
424
{% endfor %}
425

426 427 428 429
[directory]
recipe = slapos.cookbook:mkdirectory
bin = ${buildout:directory}/bin/
srv = ${buildout:directory}/srv/
430 431 432 433
backup = ${:srv}/backup
# CAUCASE directories
caucased = ${:srv}/caucased
backup-caucased = ${:backup}/caucased
434 435
# NGINX
rejected-var = ${:var}/rejected-nginx
436 437 438

{% if aikc_enabled %}
[directory]
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
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
455
organization = {{ instance_parameter_dict['root-instance-title'] }}
456 457 458
organizational_unit = Automatic Internal Kedifa Caucase CSR
command =
  if [ ! -f ${:csr} ] && [ ! -f ${:key} ]  ; then
459
    {{ software_parameter_dict['openssl'] }} req -new -sha256 \
460 461 462 463 464 465 466
      -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}
467
{#- Can be stopped on error, as does not rely on self provided service #}
468 469 470 471 472 473 474 475
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
476 477
template = inline:#!{{ software_parameter_dict['dash'] }}/bin/dash
  exec {{ software_parameter_dict['bin_directory'] }}/caucase \
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
{# 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
494 495
{#- The called command is smart enough to survive errors and retry #}
stop-on-error = False
496 497 498 499 500 501 502 503 504 505 506
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

507
{% do part_list.append('aikc-user-caucase-updater') %}
508
{% do part_list.append('aikc-user-caucase-updater-promise') %}
509 510
{{ caucase.updater(
     prefix='aikc-user-caucase-updater',
511
     buildout_bin_directory=software_parameter_dict['bin_directory'],
512 513 514 515 516 517 518 519 520 521 522
     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',
)}}


523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
[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
542
template = inline:#!{{ software_parameter_dict['dash'] }}/bin/dash
543 544 545 546 547 548 549 550
  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
551
    csr_id=`{{ software_parameter_dict['curl'] }}/bin/curl -s -k -g \
552 553 554 555 556 557 558 559 560 561 562 563
{% 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
564 565
{#- The called command is smart enough to survive errors and retry #}
stop-on-error = False
566 567 568 569
command =
  ${aikc-{{ csr }}-wrapper:rendered}
update-command = ${:command}
{% endfor %}
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
{% endif %} {# if aikc_enabled #}

{% if aibcc_enabled %}
[directory]
aibcc = ${:srv}/aibcc

[aibcc-config]
caucase-url = {{ caucase_url }}

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

[aibcc-user-csr]
recipe = plone.recipe.command
590
organization = {{ instance_parameter_dict['root-instance-title'] }}
591 592 593
organizational_unit = Automatic Sign Backend Client Caucase CSR
command =
  if [ ! -f ${:csr} ] && [ ! -f ${:key} ]  ; then
594
    {{ software_parameter_dict['openssl'] }} req -new -sha256 \
595 596 597 598 599 600 601
      -newkey rsa:2048 -nodes -keyout ${:key} \
      -subj "/O=${:organization}/OU=${:organizational_unit}" \
      -out ${:csr}
  fi
update-command = ${:command}
csr = ${aibcc-config:csr}
key = ${aibcc-config:key}
602
{#- Can be stopped on error, as does not rely on self provided service #}
603 604 605 606 607 608 609 610
stop-on-error = True


[aibcc-caucase-wrapper]
{# jinja2 instead of wrapper is used with context to remove py'u' #}
recipe = slapos.recipe.template:jinja2
context =
  key caucase_url aibcc-config:caucase-url
611 612
template = inline:#!{{ software_parameter_dict['dash'] }}/bin/dash
  exec {{ software_parameter_dict['bin_directory'] }}/caucase \
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
{# raw block to use context #}
{% raw %}
  --ca-url {{ caucase_url }} \
{% endraw %}
  --ca-crt ${aibcc-config:ca-certificate} \
  --user-ca-crt ${aibcc-config:user-ca-certificate} \
  --user-crl ${aibcc-config:user-crl} \
  --crl ${aibcc-config:crl} \
  "$@"

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

{% do part_list.append('aibcc-create-user') %}
[aibcc-create-user]
recipe = plone.recipe.command
# the caucase for this part is provided in this profile, so we can't fail
# as otherwise caucase will never be started...
631
{#- XXX: Create promise #}
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
stop-on-error = False
update-command = ${:command}
command =
  if ! [ -f ${aibcc-config:user-created} ] ; then
    ${aibcc-caucase-wrapper:rendered} --mode user --send-csr ${aibcc-user-csr:csr} > ${aibcc-config:csr_id} || exit 1
    cut -d ' ' -f 1 ${aibcc-config:csr_id} || exit 1
    csr_id=`cut -d ' ' -f 1 ${aibcc-config:csr_id}`
    sleep 1
    ${aibcc-caucase-wrapper:rendered} --mode user --get-crt $csr_id ${aibcc-config:key} || exit 1
    touch ${aibcc-config:user-created}
  fi

{% do part_list.append('aibcc-user-caucase-updater') %}
{% do part_list.append('aibcc-user-caucase-updater-promise') %}
{{ caucase.updater(
     prefix='aibcc-user-caucase-updater',
648
     buildout_bin_directory=software_parameter_dict['bin_directory'],
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
     updater_path='${directory:service}/aibcc-user-caucase-updater',
     url='${aibcc-config:caucase-url}',
     data_dir='${directory:srv}/caucase-updater',
     crt_path='${aibcc-config:key}',
     ca_path='${aibcc-config:user-ca-certificate}',
     crl_path='${aibcc-config:user-crl}',
     key_path='${aibcc-config:key}',
     mode='user',
)}}

[aibcc-check-certificate]
recipe = slapos.recipe.template:jinja2
rendered = ${directory:bin}/aibcc-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 %}
[aibcc-{{ 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-backend-client-csr_id-url
  key csr_id_certificate request-{{ csr }}:connection-csr_id-certificate
678
template = inline:#!{{ software_parameter_dict['dash'] }}/bin/dash
679 680 681 682 683 684 685 686
  test -f ${directory:aibcc}/{{ csr }}-done && exit 0
  ${buildout:executable} ${aibcc-check-certificate:rendered} \
{# raw block to use context #}
{% raw %}
  {{ csr_id_url }} \
  """{{ csr_id_certificate }}"""
{% endraw %}
  if [ $? = 0 ]; then
687
    csr_id=`{{ software_parameter_dict['curl'] }}/bin/curl -s -k -g \
688 689 690 691 692 693 694 695 696 697 698 699
{% raw %}
  {{ csr_id_url }} \
{% endraw %}
  ` || exit 1
    ${aibcc-caucase-wrapper:rendered} --user-key ${aibcc-config:key} --sign-csr $csr_id && touch ${directory:aibcc}/{{ csr }}-done
  fi
rendered = ${directory:bin}/aibcc-{{ csr }}-wrapper
mode = 0700

{% do part_list.append('aibcc-%s' % (csr,)) %}
[aibcc-{{ csr }}]
recipe = plone.recipe.command
700 701
{#- The called command is smart enough to survive errors and retry #}
stop-on-error = False
702 703 704 705 706
command =
  ${aibcc-{{ csr }}-wrapper:rendered}
update-command = ${:command}
{% endfor %}
{% endif %} {# if aibcc_enabled #}
707

708 709 710 711 712
[rejected-slave-json]
recipe = slapos.recipe.template:jinja2
filename = rejected-slave.json
directory = ${directory:promise-output}
rendered = ${:directory}/${:filename}
713
template = {{ software_parameter_dict['template_empty'] }}
714
{% if rejected_slave_title_dict %}
715 716
{# 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)) }}
717 718 719 720 721 722 723 724 725 726 727
{% else %}
content =
{% endif %}
context =
  key content :content

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

[rejected-slave-publish-configuration]
728
ip = {{ instance_parameter_dict['ipv6-random'] }}
729 730 731 732 733 734
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
735 736
command-line = {{ software_parameter_dict['nginx'] }}
  -c ${rejected-slave-template:rendered}
737 738

wrapper-path = ${directory:service}/rejected-slave-publish
739
hash-existing-files =
740
  ${buildout:directory}/software_release/buildout.cfg
741
hash-files =
742 743 744 745 746 747 748 749
  ${rejected-slave-template:rendered}
  ${rejected-slave-certificate:certificate}

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

750
{#- Can be stopped on error, as does not rely on self provided service #}
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
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

767 768
[rejected-slave-htpasswd]
recipe = plone.recipe.command
769
{#- Can be stopped on error, as does not rely on self provided service #}
770 771 772 773 774
stop-on-error = True
file = ${directory:var}/nginx-rejected.htpasswd
command = {{ software_parameter_dict['htpasswd'] }} -cb ${:file} ${rejected-slave-password:user} ${rejected-slave-password:passwd}
update-command = ${:command}

775 776
[rejected-slave-template]
recipe = slapos.recipe.template:jinja2
777 778
var = ${directory:rejected-var}
pid = ${directory:var}/nginx-rejected.pid
779
template = inline:
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
  daemon off;
  pid ${:pid};
  error_log stderr;
  events {
  }
  http {
    include {{ software_parameter_dict['nginx_mime'] }};
    server {
      server_name_in_redirect off;
      port_in_redirect off;
      error_log stderr;
      access_log /dev/null;
      listen [${rejected-slave-publish-configuration:ip}]:${rejected-slave-publish-configuration:port} ssl;
      ssl_certificate ${rejected-slave-certificate:certificate};
      ssl_certificate_key ${rejected-slave-certificate:certificate};
      default_type application/octet-stream;
      client_body_temp_path ${:var} 1 2;
      proxy_temp_path ${:var} 1 2;
      fastcgi_temp_path ${:var} 1 2;
      uwsgi_temp_path ${:var} 1 2;
      scgi_temp_path ${:var} 1 2;

      location / {
        alias ${rejected-slave-json:directory}/;
        autoindex off;
        sendfile on;
        sendfile_max_chunk 1m;
        auth_basic "Rejected slave template";
        auth_basic_user_file ${rejected-slave-htpasswd:file};
      }
    }
811 812
  }

813
rendered = ${directory:etc}/nginx-rejected-slave.conf
814 815

[promise-rejected-slave-publish-ip-port]
816
<= monitor-promise-base
817 818 819 820 821 822
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]
823
<= monitor-promise-base
824 825 826 827 828 829 830
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}

831 832 833 834
[caucased-backend-client]
hash-existing-files = ${buildout:directory}/software_release/buildout.cfg
{{  caucase.caucased(
      prefix='caucased-backend-client',
835
      buildout_bin_directory=software_parameter_dict['bin_directory'],
836 837 838 839 840 841 842 843 844
      caucased_path='${directory:service}/caucased-backend-client',
      backup_dir='${directory:backup-caucased}',
      data_dir='${directory:caucased}',
      netloc=caucase_netloc,
      service_auto_approve_count=0,
      user_auto_approve_count=1,
      key_len=2048,
)}}

845
[buildout]
846
extends =
847 848
  {{ software_parameter_dict['profile_common'] }}
  {{ software_parameter_dict['profile_monitor2'] }}
849 850 851 852
parts =
  monitor-base
  publish-slave-information
  publish-information
853
  request-kedifa
854 855
  rejected-slave-promise
  promise-rejected-slave-publish-ip-port
856 857
  caucased-backend-client
  caucased-backend-client-promise
858
{% for part in part_list %}
859
{{ '  %s' % part }}
860
{% endfor %}
861
#      publish-information
862
{% endif %}