|
| 1 | +const assert = require('assert'); |
| 2 | +const fs = require('fs'); |
| 3 | +const os = require('os'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +const { generateKafkaAuthObject } = require('../../../../extensions/notification/utils/auth'); |
| 7 | + |
| 8 | + |
| 9 | +describe('generateKafkaAuthObject', () => { |
| 10 | + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'backbeat-test-')); |
| 11 | + const currentConfDir = process.env.CONF_DIR; |
| 12 | + |
| 13 | + before(() => { |
| 14 | + fs.mkdirSync(path.join(tempDir, 'ssl'), { recursive: true }); |
| 15 | + [ |
| 16 | + 'keytab.keytab', |
| 17 | + 'ca-cert.pem', |
| 18 | + 'client-cert.pem', |
| 19 | + 'client-key.pem' |
| 20 | + ].forEach(file => fs.writeFileSync(path.join(tempDir, 'ssl', file), `fake-${file}`)); |
| 21 | + fs.writeFileSync( |
| 22 | + path.join(tempDir, 'ssl', 'credentials.json'), |
| 23 | + JSON.stringify({ |
| 24 | + username: 'testuser', |
| 25 | + password: 'testpassword' |
| 26 | + } |
| 27 | + )); |
| 28 | + process.env.CONF_DIR = tempDir; |
| 29 | + }); |
| 30 | + |
| 31 | + after(() => { |
| 32 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 33 | + process.env.CONF_DIR = currentConfDir; |
| 34 | + }); |
| 35 | + |
| 36 | + const testCases = [ |
| 37 | + { |
| 38 | + description: 'empty auth object', |
| 39 | + valid: true, |
| 40 | + input: {}, |
| 41 | + expected: {}, |
| 42 | + }, |
| 43 | + // SSL configurations |
| 44 | + { |
| 45 | + description: 'SSL-only with ssl flag enabled', |
| 46 | + valid: true, |
| 47 | + input: { |
| 48 | + ssl: true |
| 49 | + }, |
| 50 | + expected: { |
| 51 | + 'security.protocol': 'ssl', |
| 52 | + }, |
| 53 | + }, |
| 54 | + { |
| 55 | + description: 'SSL-only with ssl flag disabled', |
| 56 | + valid: true, |
| 57 | + input: { |
| 58 | + ssl: false |
| 59 | + }, |
| 60 | + expected: {}, |
| 61 | + }, |
| 62 | + { |
| 63 | + description: 'SSL with custom certificates', |
| 64 | + valid: true, |
| 65 | + input: { |
| 66 | + ssl: true, |
| 67 | + ca: 'ca-cert.pem', |
| 68 | + client: 'client-cert.pem', |
| 69 | + key: 'client-key.pem', |
| 70 | + keyPassword: 'test-password', |
| 71 | + }, |
| 72 | + expected: { |
| 73 | + 'security.protocol': 'ssl', |
| 74 | + 'ssl.ca.location': path.join(tempDir, 'ssl', 'ca-cert.pem'), |
| 75 | + 'ssl.certificate.location': path.join(tempDir, 'ssl', 'client-cert.pem'), |
| 76 | + 'ssl.key.location': path.join(tempDir, 'ssl', 'client-key.pem'), |
| 77 | + 'ssl.key.password': 'test-password', |
| 78 | + }, |
| 79 | + }, |
| 80 | + // Kerberos authentication |
| 81 | + { |
| 82 | + description: 'Kerberos authentication with valid parameters', |
| 83 | + valid: true, |
| 84 | + input: { |
| 85 | + type: 'kerberos', |
| 86 | + protocol: 'SASL_PLAINTEXT', |
| 87 | + keytab: 'keytab.keytab', |
| 88 | + principal: 'test-principal', |
| 89 | + serviceName: 'test-service', |
| 90 | + }, |
| 91 | + expected: { |
| 92 | + 'sasl.mechanisms': 'GSSAPI', |
| 93 | + 'security.protocol': 'SASL_PLAINTEXT', |
| 94 | + 'sasl.kerberos.service.name': 'test-service', |
| 95 | + 'sasl.kerberos.principal': 'test-principal', |
| 96 | + 'sasl.kerberos.keytab': path.join(tempDir, 'ssl', 'keytab.keytab'), |
| 97 | + 'sasl.kerberos.kinit.cmd': `kinit -k test-principal -t ${path.join(tempDir, 'ssl', 'keytab.keytab')}`, |
| 98 | + }, |
| 99 | + }, |
| 100 | + { |
| 101 | + description: 'Kerberos authentication with missing keytab file', |
| 102 | + valid: false, |
| 103 | + input: { |
| 104 | + type: 'kerberos', |
| 105 | + protocol: 'SASL_PLAINTEXT', |
| 106 | + keytab: 'nonexistent.keytab', |
| 107 | + principal: 'test-principal', |
| 108 | + serviceName: 'test-service', |
| 109 | + }, |
| 110 | + }, |
| 111 | + { |
| 112 | + description: 'Kerberos authentication with unsupported protocol', |
| 113 | + valid: false, |
| 114 | + input: { |
| 115 | + type: 'kerberos', |
| 116 | + protocol: 'UNSUPPORTED_PROTOCOL', |
| 117 | + keytab: 'keytab.keytab', |
| 118 | + principal: 'test-principal', |
| 119 | + serviceName: 'test-service', |
| 120 | + }, |
| 121 | + }, |
| 122 | + // Basic authentication |
| 123 | + { |
| 124 | + description: 'Basic authentication with valid parameters', |
| 125 | + valid: true, |
| 126 | + input: { |
| 127 | + type: 'basic', |
| 128 | + protocol: 'SASL_PLAINTEXT', |
| 129 | + credentialsFile: 'credentials.json', |
| 130 | + }, |
| 131 | + expected: { |
| 132 | + 'security.protocol': 'SASL_PLAINTEXT', |
| 133 | + 'sasl.mechanisms': 'PLAIN', |
| 134 | + 'sasl.username': 'testuser', |
| 135 | + 'sasl.password': 'testpassword', |
| 136 | + }, |
| 137 | + }, |
| 138 | + { |
| 139 | + description: 'Basic authentication with missing credentials file', |
| 140 | + valid: false, |
| 141 | + input: { |
| 142 | + type: 'basic', |
| 143 | + protocol: 'SASL_PLAINTEXT', |
| 144 | + credentialsFile: 'nonexistent.json', |
| 145 | + }, |
| 146 | + }, |
| 147 | + { |
| 148 | + description: 'Basic authentication with unsupported protocol', |
| 149 | + valid: false, |
| 150 | + input: { |
| 151 | + type: 'basic', |
| 152 | + protocol: 'UNSUPPORTED_PROTOCOL', |
| 153 | + credentialsFile: 'credentials.json', |
| 154 | + }, |
| 155 | + } |
| 156 | + ]; |
| 157 | + |
| 158 | + testCases.forEach(({ description, input, expected, valid }) => { |
| 159 | + it(description, () => { |
| 160 | + const tester = valid ? assert.doesNotThrow : assert.throws; |
| 161 | + tester(() => { |
| 162 | + const result = generateKafkaAuthObject(input); |
| 163 | + if (valid) { |
| 164 | + assert.deepStrictEqual(result, expected); |
| 165 | + } |
| 166 | + }); |
| 167 | + }); |
| 168 | + }); |
| 169 | +}); |
0 commit comments