|
5 | 5 | "context" |
6 | 6 | "crypto/sha256" |
7 | 7 | "encoding/hex" |
| 8 | + "encoding/json" |
8 | 9 | "fmt" |
9 | 10 | "io" |
10 | 11 | "log" |
@@ -92,6 +93,110 @@ func uploadFile(filePath, destination, securityAgentAPIEndpoint, securityAgentAP |
92 | 93 | return fmt.Errorf("failed to upload file to S3: %v", err) |
93 | 94 | } |
94 | 95 |
|
| 96 | + type Metadata struct { |
| 97 | + OriginalFileName string `json:"original_file_name"` |
| 98 | + FileContentPath string `json:"file_content_path"` |
| 99 | + GitHubMetadata map[string]string `json:"github_metadata"` |
| 100 | + Tags map[string]string `json:"tags"` |
| 101 | + } |
| 102 | + |
| 103 | + var metadata Metadata |
| 104 | + metadata.OriginalFileName = filepath.Base(filePath) |
| 105 | + metadata.FileContentPath = destination |
| 106 | + metadata.GitHubMetadata = make(map[string]string) |
| 107 | + metadata.Tags = make(map[string]string) |
| 108 | + |
| 109 | + githubActionEnvVars := []string{ |
| 110 | + "GITHUB_JOB", |
| 111 | + "GITHUB_REF", |
| 112 | + "GITHUB_SHA", |
| 113 | + "GITHUB_REPOSITORY", |
| 114 | + "GITHUB_REPOSITORY_OWNER", |
| 115 | + "GITHUB_REPOSITORY_OWNER_ID", |
| 116 | + "GITHUB_RUN_ID", |
| 117 | + "GITHUB_RUN_NUMBER", |
| 118 | + "GITHUB_RETENTION_DAYS", |
| 119 | + "GITHUB_RUN_ATTEMPT", |
| 120 | + "GITHUB_ACTOR_ID", |
| 121 | + "GITHUB_ACTOR", |
| 122 | + "GITHUB_WORKFLOW", |
| 123 | + "GITHUB_HEAD_REF", |
| 124 | + "GITHUB_BASE_REF", |
| 125 | + "GITHUB_EVENT_NAME", |
| 126 | + "GITHUB_SERVER_URL", |
| 127 | + "GITHUB_API_URL", |
| 128 | + "GITHUB_GRAPHQL_URL", |
| 129 | + "GITHUB_REF_NAME", |
| 130 | + "GITHUB_REF_PROTECTED", |
| 131 | + "GITHUB_REF_TYPE", |
| 132 | + "GITHUB_WORKFLOW_REF", |
| 133 | + "GITHUB_WORKFLOW_SHA", |
| 134 | + "GITHUB_REPOSITORY_ID", |
| 135 | + "GITHUB_TRIGGERING_ACTOR", |
| 136 | + "GITHUB_WORKSPACE", |
| 137 | + "GITHUB_ACTION", |
| 138 | + "GITHUB_EVENT_PATH", |
| 139 | + "GITHUB_ACTION_REPOSITORY", |
| 140 | + "GITHUB_ACTION_REF", |
| 141 | + "GITHUB_PATH", |
| 142 | + "GITHUB_ENV", |
| 143 | + "GITHUB_STEP_SUMMARY", |
| 144 | + "GITHUB_STATE", |
| 145 | + "GITHUB_OUTPUT", |
| 146 | + "RUNNER_OS", |
| 147 | + "RUNNER_ARCH", |
| 148 | + "RUNNER_NAME", |
| 149 | + "RUNNER_ENVIRONMENT", |
| 150 | + "RUNNER_TOOL_CACHE", |
| 151 | + "RUNNER_TEMP", |
| 152 | + "RUNNER_WORKSPACE", |
| 153 | + "ACTIONS_RUNTIME_URL", |
| 154 | + "ACTIONS_RUNTIME_TOKEN", |
| 155 | + "ACTIONS_CACHE_URL", |
| 156 | + "ACTIONS_ID_TOKEN_REQUEST_URL", |
| 157 | + "ACTIONS_ID_TOKEN_REQUEST_TOKEN", |
| 158 | + "ACTIONS_RESULTS_URL", |
| 159 | + "GITHUB_ACTIONS", |
| 160 | + "CI", |
| 161 | + } |
| 162 | + |
| 163 | + if os.Getenv("GITHUB_ACTIONS") == "true" { |
| 164 | + metadataUploadPath := "metadata/" + os.Getenv("GITHUB_REPOSITORY") + "/" + os.Getenv("GITHUB_REF") + "/" + os.Getenv("GITHUB_SHA") + ".json" |
| 165 | + for _, envVar := range githubActionEnvVars { |
| 166 | + metadata.GitHubMetadata[envVar] = os.Getenv(envVar) |
| 167 | + } |
| 168 | + |
| 169 | + log.Println("Getting presigned URL for metadata upload", metadataUploadPath) |
| 170 | + presignedURL, err := getPresignedUploadURL(metadataUploadPath, securityAgentAPIEndpoint, securityAgentAPIKey) |
| 171 | + if err != nil { |
| 172 | + return fmt.Errorf("failed to get presigned upload URL: %v", err) |
| 173 | + } |
| 174 | + |
| 175 | + metadataJSON, err := json.Marshal(metadata) |
| 176 | + if err != nil { |
| 177 | + return fmt.Errorf("failed to marshal metadata: %v", err) |
| 178 | + } |
| 179 | + |
| 180 | + metadataFile, err := os.CreateTemp("", "metadata.json") |
| 181 | + if err != nil { |
| 182 | + return fmt.Errorf("failed to create temp metadata file: %v", err) |
| 183 | + } |
| 184 | + defer os.Remove(metadataFile.Name()) |
| 185 | + |
| 186 | + _, err = metadataFile.Write(metadataJSON) |
| 187 | + if err != nil { |
| 188 | + return fmt.Errorf("failed to write metadata to temp file: %v", err) |
| 189 | + } |
| 190 | + |
| 191 | + log.Println("Uploading metadata to S3") |
| 192 | + err = uploadFileToS3(metadataFile.Name(), presignedURL) |
| 193 | + if err != nil { |
| 194 | + return fmt.Errorf("failed to upload file to S3: %v", err) |
| 195 | + } |
| 196 | + |
| 197 | + log.Println("Metadata upload completed successfully") |
| 198 | + } |
| 199 | + |
95 | 200 | log.Printf("File uploaded successfully to: %s", destination) |
96 | 201 | return nil |
97 | 202 | } |
@@ -139,8 +244,7 @@ func getPresignedUploadURL(destination, securityAgentAPIEndpoint, securityAgentA |
139 | 244 | } |
140 | 245 |
|
141 | 246 | // uploadFileToS3 uploads the file to S3 using the presigned URL |
142 | | -func uploadFileToS3(filePath, presignedURL string) error { |
143 | | - // Open the file |
| 247 | +func uploadFileToS3(filePath string, presignedURL string) error { |
144 | 248 | file, err := os.Open(filePath) |
145 | 249 | if err != nil { |
146 | 250 | return fmt.Errorf("failed to open file: %v", err) |
|
0 commit comments