-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaction_inputs.go
169 lines (152 loc) · 6.26 KB
/
action_inputs.go
1
2
3
4
5
6
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
package main
import (
"flag"
"fmt"
"sort"
"strings"
"github.com/rs/zerolog/log"
)
// Required Action inputs
var (
inputRepoToken *string = flag.String(inputKeyRepoToken, "", "GITHUB_TOKEN or a Personal Access Token")
inputWorkflowRunID *int64 = flag.Int64(inputKeyWorkflowRunID, 0, "GitHub Actions Workflow Run ID")
inputDestination *string = flag.String(inputKeyDestination, "", "The service to export workflow logs to")
)
// Inputs for S3
var (
inputAWSAccessKeyID *string = flag.String(inputKeyAWSAccessKeyID, "", "AWS Access Key ID")
inputAWSSecretAccessKey *string = flag.String(inputKeyAWSSecretAccessKey, "", "AWS Secret Access Key")
inputAWSSessionToken *string = flag.String(inputKeyAWSSessionToken, "", "AWS Session Token for temporary credentials")
inputAWSRegion *string = flag.String(inputKeyAWSRegion, "us-east-1", "AWS Region for the S3 bucket")
inputS3BucketName *string = flag.String(inputKeyS3BucketName, "", "S3 bucket name")
inputS3Key *string = flag.String(inputKeyS3Key, "", "S3 key")
)
// Required inputs for Azure Blob Storage
var (
inputAzureStorageAccountName *string = flag.String(inputKeyAzureStorageAccountName, "", "Storage account name")
inputAzureStorageAccountKey *string = flag.String(inputKeyAzureStorageAccountKey, "", "Storage account key")
inputContainerName *string = flag.String(inputKeyContainerName, "", "Azure blob storage container name")
inputBlobName *string = flag.String(inputKeyBlobName, "", "Azure blob name")
)
// Inputs for Google Cloud Storage
var (
inputCloudStorageBucketName *string = flag.String(inputKeyCloudStorageBucketName, "", "Google Cloud Storage bucket name")
inputCloudStorageObjectName *string = flag.String(inputKeyCloudStorageObjectName, "", "Google Cloud Storage object name")
)
// S3ActionInputs contains inputs used for the `s3` destination
type S3ActionInputs struct {
awsAccessKeyID string
awsSecretAccessKey string
awsSessionToken string // optional
awsRegion string
bucketName string
key string
}
// BlobStorageActionInputs contains inputs required for the `blobstorage` destination
type BlobStorageActionInputs struct {
storageAccountName string
storageAccountKey string
containerName string
blobName string
}
// CloudStorageActionInputs contains inputs required for the `cloudstorage` destination
type CloudStorageActionInputs struct {
bucketName string
objectName string
}
// ActionInputs contains all the pertinent inputs for this GitHub Action. For a given destination, its corresponding
// struct field (e.g. s3Inputs for the `s3` destination) is assumed to be non-nil.
type ActionInputs struct {
repoToken string
workflowRunID int64
destination string
s3Inputs *S3ActionInputs
blobStorageInputs *BlobStorageActionInputs
cloudStorageInputs *CloudStorageActionInputs
}
// validateActionInputs validates input combinations that cannot be checked at the action-level.
// In particular, ensures that the destination is valid and any other inputs required for that destination are present.
func validateActionInputs() (ActionInputs, error) {
var matchedDestination string
for _, destination := range supportedDestinations {
if strings.EqualFold(destination, *inputDestination) {
matchedDestination = destination
log.Debug().Str("matchedDestination", destination).Msg("Matched input with a supported destination")
break
}
}
if matchedDestination == "" {
return ActionInputs{}, fmt.Errorf(
"supplied destination %s is invalid. Supported values are: %s",
*inputDestination,
strings.Join(supportedDestinations, ", "),
)
}
var inputFlagsToAssertNotEmpty map[string]string
var s3Inputs *S3ActionInputs
var blobStorageInputs *BlobStorageActionInputs
var cloudStorageInputs *CloudStorageActionInputs
if matchedDestination == amazonS3Destination {
log.Debug().Msg("Validating Action inputs for S3")
s3Inputs = &S3ActionInputs{
awsAccessKeyID: *inputAWSAccessKeyID,
awsSecretAccessKey: *inputAWSSecretAccessKey,
awsSessionToken: *inputAWSSessionToken, // optional
awsRegion: *inputAWSRegion,
bucketName: *inputS3BucketName,
key: *inputS3Key,
}
inputFlagsToAssertNotEmpty = map[string]string{
inputKeyAWSAccessKeyID: *inputAWSAccessKeyID,
inputKeyAWSSecretAccessKey: *inputAWSSecretAccessKey,
inputKeyAWSRegion: *inputAWSRegion,
inputKeyS3BucketName: *inputS3BucketName,
inputKeyS3Key: *inputS3Key,
}
}
if matchedDestination == azureBlobStorageDestination {
log.Debug().Msg("Validating Action inputs for Blob Storage")
blobStorageInputs = &BlobStorageActionInputs{
storageAccountName: *inputAzureStorageAccountName,
storageAccountKey: *inputAzureStorageAccountKey,
containerName: *inputContainerName,
blobName: *inputBlobName,
}
inputFlagsToAssertNotEmpty = map[string]string{
inputKeyAzureStorageAccountName: *inputAzureStorageAccountName,
inputKeyAzureStorageAccountKey: *inputAzureStorageAccountKey,
inputKeyContainerName: *inputContainerName,
inputKeyBlobName: *inputBlobName,
}
}
if matchedDestination == googleCloudStorageDestination {
log.Debug().Msg("Validating Action inputs for Cloud Storage")
cloudStorageInputs = &CloudStorageActionInputs{
bucketName: *inputCloudStorageBucketName,
objectName: *inputCloudStorageObjectName,
}
inputFlagsToAssertNotEmpty = map[string]string{
inputKeyCloudStorageBucketName: *inputCloudStorageBucketName,
inputKeyCloudStorageObjectName: *inputCloudStorageObjectName,
}
}
var emptyInputs []string
for inputName, inputValue := range inputFlagsToAssertNotEmpty {
if len(inputValue) == 0 {
emptyInputs = append(emptyInputs, inputName)
}
}
if len(emptyInputs) > 0 {
sort.Strings(emptyInputs)
return ActionInputs{}, fmt.Errorf("the following inputs are required: %s", strings.Join(emptyInputs, ", "))
}
log.Debug().Msg("Action input validation was successful")
return ActionInputs{
repoToken: *inputRepoToken,
workflowRunID: *inputWorkflowRunID,
destination: matchedDestination,
s3Inputs: s3Inputs,
blobStorageInputs: blobStorageInputs,
cloudStorageInputs: cloudStorageInputs,
}, nil
}