Module: Legate::Web::AgentAuthenticationRoutes

Defined in:
lib/legate/web/routes/agent_authentication_routes.rb

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
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
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/legate/web/routes/agent_authentication_routes.rb', line 7

def self.registered(app)
  # Add helper methods to the app
  app.helpers do
    # Helper method to get agent authentication status
    def get_agent_auth_status(agent_name)
      definition_store = instance_variable_get(:@definition_store)
      return { status: :error, message: 'Definition store unavailable' } unless definition_store

      begin
        agent_definition = definition_store.get_definition(agent_name)
        return { status: :error, message: 'Agent not found' } unless agent_definition

        auth_manager = Legate::Auth::Manager.instance
        scheme_assignments = agent_definition[:auth_scheme_assignments] || {}
        credential_assignments = agent_definition[:auth_credential_assignments] || {}
        url_mappings = agent_definition[:auth_url_mappings] || []

        # Check if agent has any authentication configured
        has_auth = !scheme_assignments.empty? || !credential_assignments.empty? || !url_mappings.empty?

        unless has_auth
          return {
            status: :warning,
            message: 'No authentication configured - using global defaults',
            details: {
              scheme_assignments: 0,
              credential_assignments: 0,
              url_mappings: 0,
              global_mappings: auth_manager.instance_variable_get(:@url_mappings)&.size || 0
            }
          }
        end

        # Validate assigned schemes and credentials exist
        issues = []
        scheme_assignments.each do |service, scheme_name|
          issues << "Scheme '#{scheme_name}' not found for service '#{service}'" unless auth_manager.get_scheme(scheme_name.to_sym)
        end

        credential_assignments.each do |service, credential_name|
          issues << "Credential '#{credential_name}' not found for service '#{service}'" unless auth_manager.get_credential(credential_name.to_sym)
        end

        if issues.any?
          return {
            status: :error,
            message: 'Authentication configuration issues found',
            details: { issues: issues }
          }
        end

        {
          status: :success,
          message: 'Authentication properly configured',
          details: {
            scheme_assignments: scheme_assignments.size,
            credential_assignments: credential_assignments.size,
            url_mappings: url_mappings.size
          }
        }
      rescue StandardError => e
        { status: :error, message: "Error checking authentication status: #{e.message}" }
      end
    end

    # Helper method to get available authentication options for an agent
    def get_agent_auth_options
      auth_manager = Legate::Auth::Manager.instance
      schemes = auth_manager.instance_variable_get(:@schemes) || {}
      credentials = auth_manager.instance_variable_get(:@credentials) || {}

      {
        schemes: schemes.map { |name, scheme|
          {
            name: name,
            type: scheme.scheme_type,
            description: get_scheme_description(scheme)
          }
        },
        credentials: credentials.map { |name, credential|
          {
            name: name,
            type: credential.auth_type,
            description: get_credential_description(credential)
          }
        }
      }
    end

    # Helper method to test agent authentication in context
    def test_agent_authentication(agent_name, test_options = {})
      definition_store = instance_variable_get(:@definition_store)
      return { success: false, error: 'Definition store unavailable' } unless definition_store

      begin
        agent_definition = definition_store.get_definition(agent_name)
        return { success: false, error: 'Agent not found' } unless agent_definition

        auth_manager = Legate::Auth::Manager.instance
        scheme_assignments = agent_definition[:auth_scheme_assignments] || {}
        credential_assignments = agent_definition[:auth_credential_assignments] || {}
        url_mappings = agent_definition[:auth_url_mappings] || []

        test_results = {
          success: true,
          tests: [],
          agent_name: agent_name,
          has_agent_auth: !scheme_assignments.empty? || !credential_assignments.empty? || !url_mappings.empty?
        }

        # Test 1: Configuration validation
        if test_results[:has_agent_auth]
          config_issues = []

          scheme_assignments.each do |service, scheme_name|
            scheme = auth_manager.get_scheme(scheme_name.to_sym)
            config_issues << "Missing scheme '#{scheme_name}'" unless scheme
          end

          credential_assignments.each do |service, credential_name|
            credential = auth_manager.get_credential(credential_name.to_sym)
            config_issues << "Missing credential '#{credential_name}'" unless credential
          end

          if config_issues.empty?
            test_results[:tests] << {
              name: 'Agent Authentication Configuration',
              status: 'passed',
              message: 'All assigned schemes and credentials are available'
            }
          else
            test_results[:success] = false
            test_results[:tests] << {
              name: 'Agent Authentication Configuration',
              status: 'failed',
              message: "Configuration issues: #{config_issues.join(', ')}"
            }
          end
        else
          test_results[:tests] << {
            name: 'Agent Authentication Configuration',
            status: 'warning',
            message: 'No agent-specific authentication configured - using global defaults'
          }
        end

        # Test 2: URL mapping resolution (if test URL provided)
        if test_options[:test_url]
          url = test_options[:test_url]
          resolved_auth = resolve_agent_authentication(agent_name, url)

          test_results[:tests] << if resolved_auth[:scheme] && resolved_auth[:credential]
                                    {
                                      name: 'URL Authentication Resolution',
                                      status: 'passed',
                                      message: "URL '#{url}' resolves to scheme '#{resolved_auth[:scheme]}' with credential '#{resolved_auth[:credential]}'"
                                    }
                                  else
                                    {
                                      name: 'URL Authentication Resolution',
                                      status: 'warning',
                                      message: "No authentication resolved for URL '#{url}'"
                                    }
                                  end
        end

        test_results
      rescue StandardError => e
        { success: false, error: "Error testing agent authentication: #{e.message}" }
      end
    end

    # Helper method to resolve authentication for an agent and URL
    def resolve_agent_authentication(agent_name, url)
      definition_store = instance_variable_get(:@definition_store)
      return { scheme: nil, credential: nil, source: :error } unless definition_store

      begin
        agent_definition = definition_store.get_definition(agent_name)
        return { scheme: nil, credential: nil, source: :error } unless agent_definition

        auth_manager = Legate::Auth::Manager.instance

        # First check agent-specific URL mappings
        agent_url_mappings = agent_definition[:auth_url_mappings] || []
        agent_url_mappings.each do |mapping|
          pattern = mapping['pattern'] || mapping[:pattern]
          next unless pattern

          matched = if pattern.is_a?(Regexp)
                      !!(url =~ pattern)
                    elsif pattern.include?('*')
                      regex = Regexp.new('^' + Regexp.escape(pattern).gsub('\\*', '.*') + '$')
                      !!(url =~ regex)
                    else
                      url == pattern
                    end

          next unless matched

          scheme_name = mapping['scheme_name'] || mapping[:scheme_name]
          credential_name = mapping['credential_name'] || mapping[:credential_name]
          return {
            scheme: scheme_name,
            credential: credential_name,
            source: :agent_mapping
          }
        end

        # Then check global URL mappings
        global_mappings = auth_manager.instance_variable_get(:@url_mappings) || []
        global_mappings.each do |mapping|
          pattern = mapping[:pattern]
          next unless pattern

          matched = if pattern.is_a?(Regexp)
                      !!(url =~ pattern)
                    elsif pattern.to_s.include?('*')
                      regex = Regexp.new('^' + Regexp.escape(pattern.to_s).gsub('\\*', '.*') + '$')
                      !!(url =~ regex)
                    else
                      url == pattern.to_s
                    end

          if matched
            return {
              scheme: mapping[:scheme_name],
              credential: mapping[:credential_name],
              source: :global_mapping
            }
          end
        end

        { scheme: nil, credential: nil, source: :none }
      rescue StandardError => e
        { scheme: nil, credential: nil, source: :error }
      end
    end
  end

  # GET /agents/:name/auth - Agent-specific authentication configuration
  app.get '/agents/:name/auth' do |name|
    logger.info("GET /agents/#{name}/auth route handler entered (from AgentAuthenticationRoutes)")
    content_type :html

    definition_store = instance_variable_get(:@definition_store)
    halt 503, 'Definition Store unavailable.' unless definition_store

    agent_definition = definition_store.get_definition(name)
    halt 404, 'Agent not found.' unless agent_definition

    auth_status = get_agent_auth_status(name)
    auth_options = get_agent_auth_options

    agent_auth_data = {
      name: name,
      description: agent_definition[:description],
      auth_status: auth_status,
      scheme_assignments: agent_definition[:auth_scheme_assignments] || {},
      credential_assignments: agent_definition[:auth_credential_assignments] || {},
      url_mappings: agent_definition[:auth_url_mappings] || [],
      available_schemes: auth_options[:schemes],
      available_credentials: auth_options[:credentials]
    }

    instance_variable_set(:@agent_auth_data, agent_auth_data)
    slim :agent_auth
  rescue StandardError => e
    logger.error("Error in /agents/#{name}/auth route (from AgentAuthenticationRoutes): #{e.class} - #{e.message}")
    halt 500, "Error loading agent authentication configuration: #{e.message}"
  end

  # POST /agents/:name/auth/assign - Assign authentication to agent
  app.post '/agents/:name/auth/assign' do |name|
    logger.info("POST /agents/#{name}/auth/assign route handler entered (from AgentAuthenticationRoutes)")
    content_type :json

    definition_store = instance_variable_get(:@definition_store)
    halt 503, { error: 'Definition Store unavailable' }.to_json unless definition_store

    agent_definition = definition_store.get_definition(name)
    halt 404, { error: 'Agent not found' }.to_json unless agent_definition

    assignment_type = params[:assignment_type]
    service = params[:service]
    auth_name = params[:auth_name]

    halt 400, { error: 'Assignment type is required' }.to_json unless assignment_type
    halt 400, { error: 'Service is required' }.to_json unless service
    halt 400, { error: 'Authentication name is required' }.to_json unless auth_name

    begin
      case assignment_type
      when 'scheme'
        # Verify scheme exists
        auth_manager = Legate::Auth::Manager.instance
        scheme = auth_manager.get_scheme(auth_name.to_sym)
        halt 400, { error: "Scheme '#{auth_name}' not found" }.to_json unless scheme

        # Update agent's scheme assignments
        scheme_assignments = agent_definition[:auth_scheme_assignments] || {}
        scheme_assignments[service] = auth_name

        definition_store.update_definition(name, { auth_scheme_assignments: scheme_assignments })

      when 'credential'
        # Verify credential exists
        auth_manager = Legate::Auth::Manager.instance
        credential = auth_manager.get_credential(auth_name.to_sym)
        halt 400, { error: "Credential '#{auth_name}' not found" }.to_json unless credential

        # Update agent's credential assignments
        credential_assignments = agent_definition[:auth_credential_assignments] || {}
        credential_assignments[service] = auth_name

        definition_store.update_definition(name, { auth_credential_assignments: credential_assignments })

      else
        halt 400, { error: "Invalid assignment type: #{assignment_type}" }.to_json
      end

      logger.info("Successfully assigned #{assignment_type} '#{auth_name}' to service '#{service}' for agent '#{name}'")
      { success: true, message: "#{assignment_type.capitalize} '#{auth_name}' assigned to service '#{service}'" }.to_json
    rescue StandardError => e
      logger.error("Error assigning authentication to agent '#{name}': #{e.class} - #{e.message}")
      halt 500, { error: "Failed to assign authentication: #{e.message}" }.to_json
    end
  end

  # DELETE /agents/:name/auth/remove - Remove authentication from agent
  app.delete '/agents/:name/auth/remove' do |name|
    logger.info("DELETE /agents/#{name}/auth/remove route handler entered (from AgentAuthenticationRoutes)")
    content_type :json

    definition_store = instance_variable_get(:@definition_store)
    halt 503, { error: 'Definition Store unavailable' }.to_json unless definition_store

    agent_definition = definition_store.get_definition(name)
    halt 404, { error: 'Agent not found' }.to_json unless agent_definition

    assignment_type = params[:assignment_type]
    service = params[:service]

    halt 400, { error: 'Assignment type is required' }.to_json unless assignment_type
    halt 400, { error: 'Service is required' }.to_json unless service

    begin
      case assignment_type
      when 'scheme'
        scheme_assignments = agent_definition[:auth_scheme_assignments] || {}
        removed_scheme = scheme_assignments.delete(service)

        if removed_scheme
          definition_store.update_definition(name, { auth_scheme_assignments: scheme_assignments })
          logger.info("Successfully removed scheme assignment for service '#{service}' from agent '#{name}'")
          { success: true, message: "Scheme assignment for service '#{service}' removed" }.to_json
        else
          { success: false, message: "No scheme assignment found for service '#{service}'" }.to_json
        end

      when 'credential'
        credential_assignments = agent_definition[:auth_credential_assignments] || {}
        removed_credential = credential_assignments.delete(service)

        if removed_credential
          definition_store.update_definition(name, { auth_credential_assignments: credential_assignments })
          logger.info("Successfully removed credential assignment for service '#{service}' from agent '#{name}'")
          { success: true, message: "Credential assignment for service '#{service}' removed" }.to_json
        else
          { success: false, message: "No credential assignment found for service '#{service}'" }.to_json
        end

      else
        halt 400, { error: "Invalid assignment type: #{assignment_type}" }.to_json
      end
    rescue StandardError => e
      logger.error("Error removing authentication from agent '#{name}': #{e.class} - #{e.message}")
      halt 500, { error: "Failed to remove authentication: #{e.message}" }.to_json
    end
  end

  # POST /agents/:name/auth/test - Test authentication in agent context
  app.post '/agents/:name/auth/test' do |name|
    logger.info("POST /agents/#{name}/auth/test route handler entered (from AgentAuthenticationRoutes)")
    content_type :json

    definition_store = instance_variable_get(:@definition_store)
    halt 503, { error: 'Definition Store unavailable' }.to_json unless definition_store

    agent_definition = definition_store.get_definition(name)
    halt 404, { error: 'Agent not found' }.to_json unless agent_definition

    begin
      test_options = {}
      test_options[:test_url] = params[:test_url] if params[:test_url] && !params[:test_url].empty?

      test_results = test_agent_authentication(name, test_options)

      logger.info("Agent authentication test completed for '#{name}': #{test_results[:success] ? 'PASSED' : 'FAILED'}")
      test_results.to_json
    rescue StandardError => e
      logger.error("Error testing agent authentication for '#{name}': #{e.class} - #{e.message}")
      halt 500, { error: "Failed to test agent authentication: #{e.message}" }.to_json
    end
  end

  # GET /agents/:name/auth/status - Get authentication status for agent
  app.get '/agents/:name/auth/status' do |name|
    logger.info("GET /agents/#{name}/auth/status route handler entered (from AgentAuthenticationRoutes)")
    content_type :json

    definition_store = instance_variable_get(:@definition_store)
    halt 503, { error: 'Definition Store unavailable' }.to_json unless definition_store

    agent_definition = definition_store.get_definition(name)
    halt 404, { error: 'Agent not found' }.to_json unless agent_definition

    begin
      auth_status = get_agent_auth_status(name)
      auth_status.to_json
    rescue StandardError => e
      logger.error("Error getting agent authentication status for '#{name}': #{e.class} - #{e.message}")
      halt 500, { error: "Failed to get authentication status: #{e.message}" }.to_json
    end
  end

  # POST /agents/:name/auth/url-mapping - Add URL mapping for agent
  app.post '/agents/:name/auth/url-mapping' do |name|
    logger.info("POST /agents/#{name}/auth/url-mapping route handler entered (from AgentAuthenticationRoutes)")
    content_type :json

    definition_store = instance_variable_get(:@definition_store)
    halt 503, { error: 'Definition Store unavailable' }.to_json unless definition_store

    agent_definition = definition_store.get_definition(name)
    halt 404, { error: 'Agent not found' }.to_json unless agent_definition

    pattern = params[:pattern]
    pattern_type = params[:pattern_type]
    scheme_name = params[:scheme_name]
    credential_name = params[:credential_name]
    priority = params[:priority]&.to_i || 1

    halt 400, { error: 'Pattern is required' }.to_json unless pattern && !pattern.empty?
    halt 400, { error: 'Scheme name is required' }.to_json unless scheme_name && !scheme_name.empty?
    halt 400, { error: 'Credential name is required' }.to_json unless credential_name && !credential_name.empty?

    begin
      # Validate pattern
      if pattern_type == 'regex'
        begin
          Regexp.new(pattern)
        rescue RegexpError
          halt 400, { error: 'Invalid regex pattern' }.to_json
        end
      end

      # Verify scheme and credential exist
      auth_manager = Legate::Auth::Manager.instance
      scheme = auth_manager.get_scheme(scheme_name.to_sym)
      credential = auth_manager.get_credential(credential_name.to_sym)

      halt 400, { error: "Scheme '#{scheme_name}' not found" }.to_json unless scheme
      halt 400, { error: "Credential '#{credential_name}' not found" }.to_json unless credential

      # Check compatibility
      compatible_types = get_compatible_credential_types(scheme.scheme_type)
      halt 400, { error: 'Scheme and credential are not compatible' }.to_json unless compatible_types.include?(credential.auth_type.to_s) || compatible_types.include?(credential.auth_type)

      # Add URL mapping to agent
      url_mappings = agent_definition[:auth_url_mappings] || []
      new_mapping = {
        pattern: pattern_type == 'regex' ? pattern : pattern,
        pattern_type: pattern_type,
        scheme_name: scheme_name,
        credential_name: credential_name,
        priority: priority,
        active: true
      }

      url_mappings << new_mapping
      definition_store.update_definition(name, { auth_url_mappings: url_mappings })

      logger.info("Successfully added URL mapping for agent '#{name}': #{pattern} -> #{scheme_name}/#{credential_name}")
      { success: true, message: 'URL mapping added successfully', mapping_id: url_mappings.size - 1 }.to_json
    rescue StandardError => e
      logger.error("Error adding URL mapping for agent '#{name}': #{e.class} - #{e.message}")
      halt 500, { error: "Failed to add URL mapping: #{e.message}" }.to_json
    end
  end

  # DELETE /agents/:name/auth/url-mapping/:id - Remove URL mapping from agent
  app.delete '/agents/:name/auth/url-mapping/:id' do |name, mapping_id|
    logger.info("DELETE /agents/#{name}/auth/url-mapping/#{mapping_id} route handler entered (from AgentAuthenticationRoutes)")
    content_type :json

    definition_store = instance_variable_get(:@definition_store)
    halt 503, { error: 'Definition Store unavailable' }.to_json unless definition_store

    agent_definition = definition_store.get_definition(name)
    halt 404, { error: 'Agent not found' }.to_json unless agent_definition

    begin
      url_mappings = agent_definition[:auth_url_mappings] || []
      id = mapping_id.to_i

      if id >= 0 && id < url_mappings.size
        removed_mapping = url_mappings.delete_at(id)
        definition_store.update_definition(name, { auth_url_mappings: url_mappings })

        logger.info("Successfully removed URL mapping #{id} from agent '#{name}'")
        { success: true, message: 'URL mapping removed successfully' }.to_json
      else
        { success: false, message: 'URL mapping not found' }.to_json
      end
    rescue StandardError => e
      logger.error("Error removing URL mapping from agent '#{name}': #{e.class} - #{e.message}")
      halt 500, { error: "Failed to remove URL mapping: #{e.message}" }.to_json
    end
  end
end