|
| 1 | +const Kafka = require('@confluentinc/kafka-javascript'); |
| 2 | +var jwt = require('jsonwebtoken'); |
| 3 | + |
| 4 | +// This example uses the Producer for demonstration purposes. |
| 5 | +// It is the same whether you use a Consumer/AdminClient. |
| 6 | + |
| 7 | +function token_refresh(oauthbearer_config /* string - passed from config */, cb) { |
| 8 | + console.log("Called token_refresh with given config: " + oauthbearer_config); |
| 9 | + // At this point, we can use the information in the token, make |
| 10 | + // some API calls, fetch something from a file... |
| 11 | + // For the illustration, everything is hard-coded. |
| 12 | + const principal = 'admin'; |
| 13 | + // In seconds - needed by jsonwebtoken library |
| 14 | + const exp_seconds = Math.floor(Date.now() / 1000) + (60 * 60); |
| 15 | + // In milliseconds - needed by kafka-javascript. |
| 16 | + const exp_ms = exp_seconds * 1000; |
| 17 | + |
| 18 | + // For illustration, we're not signing our JWT (algorithm: none). |
| 19 | + // For production uses-cases, it should be signed. |
| 20 | + const tokenValue = jwt.sign( |
| 21 | + { 'sub': principal, exp: exp_seconds, 'scope': 'requiredScope' }, '', { algorithm: 'none' }); |
| 22 | + |
| 23 | + // SASL extensions can be passed as Map or key/value pairs in an object. |
| 24 | + const extensions = { |
| 25 | + traceId: '123' |
| 26 | + }; |
| 27 | + |
| 28 | + // The callback is called with the new token, its lifetime, and the principal. |
| 29 | + // The extensions are optional and may be omitted. |
| 30 | + console.log("Finished token_refresh, triggering callback: with tokenValue: " + |
| 31 | + tokenValue.slice(0, 10) + "..., lifetime: " + exp_ms + |
| 32 | + ", principal: " + principal + ", extensions: " + JSON.stringify(extensions)); |
| 33 | + cb( |
| 34 | + // If no token could be fetched or an error occurred, a new Error can be |
| 35 | + // and passed as the first parameter and the second parameter omitted. |
| 36 | + null, |
| 37 | + { tokenValue, lifetime: exp_ms, principal, extensions }); |
| 38 | +} |
| 39 | + |
| 40 | +function run() { |
| 41 | + const producer = new Kafka.Producer({ |
| 42 | + 'metadata.broker.list': 'localhost:60125', |
| 43 | + 'dr_cb': true, |
| 44 | + // 'debug': 'all' |
| 45 | + |
| 46 | + // Config important for OAUTHBEARER: |
| 47 | + 'security.protocol': 'SASL_PLAINTEXT', |
| 48 | + 'sasl.mechanisms': 'OAUTHBEARER', |
| 49 | + 'sasl.oauthbearer.config': 'someConfigPropertiesKey=value', |
| 50 | + 'oauthbearer_token_refresh_cb': token_refresh, |
| 51 | + }); |
| 52 | + |
| 53 | + producer.connect(); |
| 54 | + |
| 55 | + producer.on('event.log', (event) => { |
| 56 | + console.log(event); |
| 57 | + }); |
| 58 | + |
| 59 | + producer.on('ready', () => { |
| 60 | + console.log('Producer is ready!'); |
| 61 | + producer.setPollInterval(1000); |
| 62 | + console.log("Producing message."); |
| 63 | + producer.produce( |
| 64 | + 'topic', |
| 65 | + null, // partition - let partitioner choose |
| 66 | + Buffer.from('messageValue'), |
| 67 | + 'messageKey', |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + producer.on('error', (err) => { |
| 72 | + console.error("Encountered error in producer: " + err.message); |
| 73 | + }); |
| 74 | + |
| 75 | + producer.on('delivery-report', function (err, report) { |
| 76 | + console.log('delivery-report: ' + JSON.stringify(report)); |
| 77 | + // since we just want to produce one message, close shop. |
| 78 | + producer.disconnect(); |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | +run(); |
0 commit comments