20231011055535_saml_sign_encrypt.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class SamlSignEncrypt < ActiveRecord::Migration[7.0]
  3. def change
  4. # return if it's a new setup
  5. return if !Setting.exists?(name: 'system_init_done')
  6. saml_setting = Setting.find_by(name: 'auth_saml_credentials')
  7. return if !saml_setting
  8. required_attributes(saml_setting)
  9. fingerprint_help(saml_setting)
  10. add_validations(saml_setting)
  11. sign_and_encrypt_attributes(saml_setting)
  12. check_ssl_verify(saml_setting)
  13. saml_setting.save!(validate: false)
  14. end
  15. private
  16. def required_attributes(saml_setting)
  17. [1, 2, 3, 5].each do |idx|
  18. saml_setting.options[:form][idx][:required] = true
  19. end
  20. true
  21. end
  22. def fingerprint_help(saml_setting)
  23. saml_setting.options[:form][4][:help] = 'Please note that this attribute is deprecated within one of the next versions of Zammad. Use the IDP certificate instead.'
  24. true
  25. end
  26. def add_validations(saml_setting)
  27. saml_setting.preferences[:validations] = [
  28. 'Setting::Validation::Saml::RequiredAttributes',
  29. 'Setting::Validation::Saml::TLS',
  30. 'Setting::Validation::Saml::Security',
  31. ]
  32. true
  33. end
  34. def sign_and_encrypt_attributes(saml_setting)
  35. saml_setting.options[:form].insert(-2, {
  36. display: 'SSL verification',
  37. null: true,
  38. name: 'ssl_verify',
  39. tag: 'boolean',
  40. options: {
  41. true => 'yes',
  42. false => 'no',
  43. },
  44. default: true,
  45. help: 'Turning off SSL verification is a security risk and should be used only temporary. Use this option at your own risk!',
  46. },
  47. {
  48. display: 'Signing & Encrypting',
  49. null: true,
  50. name: 'security',
  51. tag: 'select',
  52. options: {
  53. 'off' => 'None',
  54. 'on' => 'Signing & Encrypting',
  55. 'sign' => 'Only Signing',
  56. 'encrypt' => 'Only Encrypting',
  57. },
  58. },
  59. {
  60. display: 'Certificate (PEM)',
  61. null: true,
  62. name: 'certificate',
  63. tag: 'textarea',
  64. placeholder: '-----BEGIN CERTIFICATE-----\n...-----END CERTIFICATE-----',
  65. },
  66. {
  67. display: 'Private key (PEM)',
  68. null: true,
  69. name: 'private_key',
  70. tag: 'textarea',
  71. placeholder: '-----BEGIN RSA PRIVATE KEY-----\n...-----END RSA PRIVATE KEY-----', # gitleaks:allow
  72. },
  73. {
  74. display: 'Private key secret',
  75. null: true,
  76. name: 'private_key_secret',
  77. tag: 'input',
  78. type: 'password',
  79. single: true,
  80. placeholder: '',
  81. })
  82. true
  83. end
  84. def check_ssl_verify(_saml_setting)
  85. if Setting.get('auth_saml_credentials').present? && Setting.get('auth_saml')
  86. Setting.set('auth_saml_credentials', Setting.get('auth_saml_credentials').merge(ssl_verify: false))
  87. end
  88. true
  89. end
  90. end