|
| 1 | +// © 2025 Platform Engineering Labs Inc. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: FSL-1.1-ALv2 |
| 4 | + |
| 5 | +package networkfirewall |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "encoding/json" |
| 10 | + "fmt" |
| 11 | + |
| 12 | + "github.com/platform-engineering-labs/formae/pkg/plugin/resource" |
| 13 | + |
| 14 | + "github.com/platform-engineering-labs/formae-plugin-aws/pkg/ccx" |
| 15 | + "github.com/platform-engineering-labs/formae-plugin-aws/pkg/cfres/prov" |
| 16 | + "github.com/platform-engineering-labs/formae-plugin-aws/pkg/cfres/registry" |
| 17 | + "github.com/platform-engineering-labs/formae-plugin-aws/pkg/config" |
| 18 | +) |
| 19 | + |
| 20 | +const loggingConfigurationType = "AWS::NetworkFirewall::LoggingConfiguration" |
| 21 | + |
| 22 | +// loggingConfigClient abstracts the CloudControl read this List synthesizes from, |
| 23 | +// so it can be mocked in unit tests. *ccx.Client satisfies this interface. |
| 24 | +type loggingConfigClient interface { |
| 25 | + ReadResource(ctx context.Context, request *resource.ReadRequest) (*resource.ReadResult, error) |
| 26 | +} |
| 27 | + |
| 28 | +// LoggingConfiguration supplies a custom List for AWS::NetworkFirewall::LoggingConfiguration. |
| 29 | +// CloudControl does not support the LIST action for this type (ListResources returns |
| 30 | +// UnsupportedActionException), so discovery cannot enumerate it the generic way. A |
| 31 | +// logging configuration is a per-firewall singleton, though, and CloudControl's GetResource |
| 32 | +// (keyed by the firewall ARN) does work. Discovery scopes this List to one firewall via the |
| 33 | +// FirewallArn list parameter; we read that firewall's logging configuration and return its |
| 34 | +// identifier when destinations are configured. Only List is registered — Create/Read/Update/ |
| 35 | +// Delete/Status fall through to the generic CloudControl path in aws.go. |
| 36 | +type LoggingConfiguration struct { |
| 37 | + cfg *config.Config |
| 38 | + // client is injectable for testing; nil means construct a real ccx.Client. |
| 39 | + client loggingConfigClient |
| 40 | +} |
| 41 | + |
| 42 | +var _ prov.Provisioner = &LoggingConfiguration{} |
| 43 | + |
| 44 | +func init() { |
| 45 | + registry.Register(loggingConfigurationType, |
| 46 | + []resource.Operation{resource.OperationList}, |
| 47 | + func(cfg *config.Config) prov.Provisioner { |
| 48 | + return &LoggingConfiguration{cfg: cfg} |
| 49 | + }) |
| 50 | +} |
| 51 | + |
| 52 | +func (l *LoggingConfiguration) getClient() (loggingConfigClient, error) { |
| 53 | + if l.client != nil { |
| 54 | + return l.client, nil |
| 55 | + } |
| 56 | + return ccx.NewClient(l.cfg) |
| 57 | +} |
| 58 | + |
| 59 | +// List returns the firewall's ARN (the logging configuration's identifier) when the |
| 60 | +// firewall named by the FirewallArn list parameter has at least one log destination |
| 61 | +// configured. A firewall with no logging, or one that no longer exists, contributes |
| 62 | +// nothing. Discovery reads each returned identifier back through the generic Read path. |
| 63 | +func (l *LoggingConfiguration) List(ctx context.Context, request *resource.ListRequest) (*resource.ListResult, error) { |
| 64 | + firewallArn := request.AdditionalProperties["FirewallArn"] |
| 65 | + if firewallArn == "" { |
| 66 | + return nil, fmt.Errorf("FirewallArn is required to list network firewall logging configurations") |
| 67 | + } |
| 68 | + |
| 69 | + client, err := l.getClient() |
| 70 | + if err != nil { |
| 71 | + return nil, fmt.Errorf("creating cloudcontrol client: %w", err) |
| 72 | + } |
| 73 | + |
| 74 | + result, err := client.ReadResource(ctx, &resource.ReadRequest{ |
| 75 | + NativeID: firewallArn, |
| 76 | + ResourceType: loggingConfigurationType, |
| 77 | + TargetConfig: request.TargetConfig, |
| 78 | + }) |
| 79 | + if err != nil { |
| 80 | + return nil, fmt.Errorf("reading logging configuration for firewall %s: %w", firewallArn, err) |
| 81 | + } |
| 82 | + if result.ErrorCode == resource.OperationErrorCodeNotFound { |
| 83 | + return &resource.ListResult{}, nil |
| 84 | + } |
| 85 | + if result.ErrorCode != "" { |
| 86 | + return nil, fmt.Errorf("reading logging configuration for firewall %s: %s", firewallArn, result.ErrorCode) |
| 87 | + } |
| 88 | + if !hasLogDestinations(result.Properties) { |
| 89 | + return &resource.ListResult{}, nil |
| 90 | + } |
| 91 | + return &resource.ListResult{NativeIDs: []string{firewallArn}}, nil |
| 92 | +} |
| 93 | + |
| 94 | +// hasLogDestinations reports whether a read logging configuration has at least one |
| 95 | +// destination configured. An empty configuration is not a discoverable resource. |
| 96 | +func hasLogDestinations(properties string) bool { |
| 97 | + if properties == "" { |
| 98 | + return false |
| 99 | + } |
| 100 | + var p struct { |
| 101 | + LoggingConfiguration struct { |
| 102 | + LogDestinationConfigs []json.RawMessage `json:"LogDestinationConfigs"` |
| 103 | + } `json:"LoggingConfiguration"` |
| 104 | + } |
| 105 | + if err := json.Unmarshal([]byte(properties), &p); err != nil { |
| 106 | + return false |
| 107 | + } |
| 108 | + return len(p.LoggingConfiguration.LogDestinationConfigs) > 0 |
| 109 | +} |
| 110 | + |
| 111 | +// The remaining Provisioner methods are unreachable: only List is registered, so |
| 112 | +// Create/Read/Update/Delete/Status always route to CloudControl in aws.go. |
| 113 | +func (l *LoggingConfiguration) Create(_ context.Context, _ *resource.CreateRequest) (*resource.CreateResult, error) { |
| 114 | + return nil, fmt.Errorf("create not implemented - cloudcontrol handles this") |
| 115 | +} |
| 116 | + |
| 117 | +func (l *LoggingConfiguration) Read(_ context.Context, _ *resource.ReadRequest) (*resource.ReadResult, error) { |
| 118 | + return nil, fmt.Errorf("read not implemented - cloudcontrol handles this") |
| 119 | +} |
| 120 | + |
| 121 | +func (l *LoggingConfiguration) Update(_ context.Context, _ *resource.UpdateRequest) (*resource.UpdateResult, error) { |
| 122 | + return nil, fmt.Errorf("update not implemented - cloudcontrol handles this") |
| 123 | +} |
| 124 | + |
| 125 | +func (l *LoggingConfiguration) Delete(_ context.Context, _ *resource.DeleteRequest) (*resource.DeleteResult, error) { |
| 126 | + return nil, fmt.Errorf("delete not implemented - cloudcontrol handles this") |
| 127 | +} |
| 128 | + |
| 129 | +func (l *LoggingConfiguration) Status(_ context.Context, _ *resource.StatusRequest) (*resource.StatusResult, error) { |
| 130 | + return nil, fmt.Errorf("status not implemented - cloudcontrol handles this") |
| 131 | +} |
0 commit comments