|
| 1 | +package chronicle |
| 2 | + |
| 3 | +import ( |
| 4 | + chronicle "github.com/form3tech-oss/terraform-provider-chronicle/client" |
| 5 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 6 | +) |
| 7 | + |
| 8 | +type ResourceFeedAmazonS3V2 struct { |
| 9 | + TerraformResource *schema.Resource |
| 10 | +} |
| 11 | + |
| 12 | +func NewResourceFeedAmazonS3V2() *ResourceFeedAmazonS3V2 { |
| 13 | + details := &schema.Resource{ |
| 14 | + Schema: map[string]*schema.Schema{ |
| 15 | + "s3_uri": { |
| 16 | + Type: schema.TypeString, |
| 17 | + Required: true, |
| 18 | + Description: `The S3 bucket URI in the format s3://bucket-name/path/.`, |
| 19 | + }, |
| 20 | + "source_delete_options": { |
| 21 | + Type: schema.TypeString, |
| 22 | + ValidateDiagFunc: validateFeedV2SourceDeleteOption, |
| 23 | + Required: true, |
| 24 | + Description: `Whether to delete source files after they have been transferred to Chronicle. Valid values are: |
| 25 | +
|
| 26 | +- NEVER: Never delete files from the source. |
| 27 | +- ON_SUCCESS: Delete files and empty directories from the source after successful ingestion.`, |
| 28 | + }, |
| 29 | + "max_lookback_days": { |
| 30 | + Type: schema.TypeInt, |
| 31 | + Optional: true, |
| 32 | + Default: 180, |
| 33 | + ValidateDiagFunc: validateMaxLookbackDays, |
| 34 | + Description: `The maximum number of days in the past to look for files. Must be between 1 and 180. Default is 180 days.`, |
| 35 | + }, |
| 36 | + "authentication": { |
| 37 | + Type: schema.TypeList, |
| 38 | + Required: true, |
| 39 | + MaxItems: 1, |
| 40 | + Description: `AWS authentication details. Use either access key credentials or IAM role ARN.`, |
| 41 | + Elem: &schema.Resource{ |
| 42 | + Schema: map[string]*schema.Schema{ |
| 43 | + "access_key_id": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Optional: true, |
| 46 | + ValidateDiagFunc: validateAWSAccessKeyID, |
| 47 | + RequiredWith: []string{"details.0.authentication.0.secret_access_key"}, |
| 48 | + ConflictsWith: []string{"details.0.authentication.0.aws_iam_role_arn"}, |
| 49 | + AtLeastOneOf: []string{ |
| 50 | + "details.0.authentication.0.access_key_id", |
| 51 | + "details.0.authentication.0.aws_iam_role_arn", |
| 52 | + }, |
| 53 | + Description: `The 20-character access key ID associated with your Amazon IAM account. Required if not using aws_iam_role_arn.`, |
| 54 | + }, |
| 55 | + "secret_access_key": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Optional: true, |
| 58 | + Sensitive: true, |
| 59 | + ValidateDiagFunc: validateAWSSecretAccessKey, |
| 60 | + RequiredWith: []string{"details.0.authentication.0.access_key_id"}, |
| 61 | + ConflictsWith: []string{"details.0.authentication.0.aws_iam_role_arn"}, |
| 62 | + Description: `The 40-character secret access key associated with your Amazon IAM account. Required if not using aws_iam_role_arn.`, |
| 63 | + }, |
| 64 | + "aws_iam_role_arn": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Optional: true, |
| 67 | + ConflictsWith: []string{"details.0.authentication.0.access_key_id", "details.0.authentication.0.secret_access_key"}, |
| 68 | + AtLeastOneOf: []string{ |
| 69 | + "details.0.authentication.0.access_key_id", |
| 70 | + "details.0.authentication.0.aws_iam_role_arn", |
| 71 | + }, |
| 72 | + Description: `ARN of the AWS IAM role configured to access S3 bucket. Use this for federated authentication instead of access keys.`, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + } |
| 79 | + description := "Creates a V2 feed from Amazon Simple Storage Service (S3). " + |
| 80 | + "This feed type uses the Google Cloud Storage Transfer Service for improved ingestion." |
| 81 | + resource := &ResourceFeedAmazonS3V2{} |
| 82 | + resource.TerraformResource = newFeedResourceSchema(details, resource, description, true) |
| 83 | + |
| 84 | + return resource |
| 85 | +} |
| 86 | + |
| 87 | +func (f *ResourceFeedAmazonS3V2) getLogType() string { |
| 88 | + return "" |
| 89 | +} |
| 90 | + |
| 91 | +func (f *ResourceFeedAmazonS3V2) expandConcreteFeedConfiguration(d *schema.ResourceData) chronicle.ConcreteFeedConfiguration { |
| 92 | + resourceDetailsInterface := readSliceFromResource(d, "details") |
| 93 | + if resourceDetailsInterface == nil { |
| 94 | + return nil |
| 95 | + } |
| 96 | + |
| 97 | + resourceDetails := resourceDetailsInterface[0].(map[string]interface{}) |
| 98 | + authenticationDetails := resourceDetails["authentication"].([]interface{})[0].(map[string]interface{}) |
| 99 | + |
| 100 | + config := &chronicle.S3V2FeedConfiguration{ |
| 101 | + S3URI: resourceDetails["s3_uri"].(string), |
| 102 | + SourceDeleteOptions: resourceDetails["source_delete_options"].(string), |
| 103 | + MaxLookbackDays: resourceDetails["max_lookback_days"].(int), |
| 104 | + Authentication: chronicle.S3V2FeedAuthentication{}, |
| 105 | + } |
| 106 | + |
| 107 | + // Check which authentication method is used |
| 108 | + if iamRoleArn, ok := authenticationDetails["aws_iam_role_arn"].(string); ok && iamRoleArn != "" { |
| 109 | + config.Authentication.AWSIAMRoleAuth = &chronicle.S3V2AWSIAMRoleAuth{ |
| 110 | + AWSIAMRoleArn: iamRoleArn, |
| 111 | + } |
| 112 | + } else { |
| 113 | + config.Authentication.AccessKeySecretAuth = &chronicle.S3V2AccessKeySecretAuth{ |
| 114 | + AccessKeyID: authenticationDetails["access_key_id"].(string), |
| 115 | + SecretAccessKey: authenticationDetails["secret_access_key"].(string), |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return config |
| 120 | +} |
| 121 | + |
| 122 | +//nolint:all |
| 123 | +func (f *ResourceFeedAmazonS3V2) flattenDetailsFromReadOperation(originalConf chronicle.ConcreteFeedConfiguration, readConf chronicle.ConcreteFeedConfiguration) []map[string]interface{} { |
| 124 | + |
| 125 | + readS3Conf := readConf.(*chronicle.S3V2FeedConfiguration) |
| 126 | + |
| 127 | + // Import Case |
| 128 | + if originalConf == nil { |
| 129 | + authMap := make(map[string]interface{}) |
| 130 | + // Only populate non-secret auth fields during import |
| 131 | + if readS3Conf.Authentication.AWSIAMRoleAuth != nil && readS3Conf.Authentication.AWSIAMRoleAuth.AWSIAMRoleArn != "" { |
| 132 | + authMap["aws_iam_role_arn"] = readS3Conf.Authentication.AWSIAMRoleAuth.AWSIAMRoleArn |
| 133 | + } |
| 134 | + |
| 135 | + // Note: access_key_id and secret_access_key are not returned by the API |
| 136 | + // and will remain empty in state after import until explicitly set by user |
| 137 | + |
| 138 | + return []map[string]interface{}{{ |
| 139 | + "s3_uri": readS3Conf.S3URI, |
| 140 | + "source_delete_options": readS3Conf.SourceDeleteOptions, |
| 141 | + "max_lookback_days": readS3Conf.MaxLookbackDays, |
| 142 | + "authentication": []map[string]interface{}{authMap}, |
| 143 | + }} |
| 144 | + } |
| 145 | + |
| 146 | + originalS3Conf := originalConf.(*chronicle.S3V2FeedConfiguration) |
| 147 | + // Default Case |
| 148 | + authMap := make(map[string]interface{}) |
| 149 | + if originalS3Conf.Authentication.AWSIAMRoleAuth != nil && originalS3Conf.Authentication.AWSIAMRoleAuth.AWSIAMRoleArn != "" { |
| 150 | + authMap["aws_iam_role_arn"] = originalS3Conf.Authentication.AWSIAMRoleAuth.AWSIAMRoleArn |
| 151 | + } |
| 152 | + if originalS3Conf.Authentication.AccessKeySecretAuth != nil { |
| 153 | + authMap["access_key_id"] = originalS3Conf.Authentication.AccessKeySecretAuth.AccessKeyID |
| 154 | + authMap["secret_access_key"] = originalS3Conf.Authentication.AccessKeySecretAuth.SecretAccessKey |
| 155 | + } |
| 156 | + |
| 157 | + return []map[string]interface{}{{ |
| 158 | + "s3_uri": readS3Conf.S3URI, |
| 159 | + "source_delete_options": originalS3Conf.SourceDeleteOptions, // not returned |
| 160 | + "max_lookback_days": readS3Conf.MaxLookbackDays, |
| 161 | + // replace authentication block with original values because they are not returned within a read request |
| 162 | + "authentication": []map[string]interface{}{authMap}, |
| 163 | + }} |
| 164 | +} |
0 commit comments