Skip to content

Commit b55c5ca

Browse files
committed
refactor
1 parent a94cf5e commit b55c5ca

File tree

13 files changed

+132
-133
lines changed

13 files changed

+132
-133
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ override.tf.json
159159
terraform.rc
160160

161161
export/*
162+
out/*
162163
zip
163164
zip/*
164165

README.md

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,31 @@ Migrate workloads from other platforms to [StackGuardian Platform](https://app.s
1414

1515
## Prerequisites
1616

17+
- An organization on [StackGuardian Platform](https://app.stackguardian.io)
18+
- Optionally, pre-configure VCS, cloud integrations or private runners to use when importing into StackGuardian Platform.
1719
- Terraform
18-
- terraform login to ensure that Terraform can interact with your Terraform Cloud/Enterprise account.
1920
- [sg-cli](https://github.com/StackGuardian/sg-cli/tree/main/shell)
2021

2122
### Perform terraform login
22-
`terraform login`
23+
Perform `terraform login` to ensure that your local Terraform can interact with your Terraform Cloud/Enterprise account.
2324

2425
### Export the resource definitions and Terraform state
2526

2627
- Choose the transformer and locate the example of `terraform.tfvars.example` and rename it to `terraform.tfvars`.
27-
- Edit that file ( terraform.tfvars) to match your context.
28+
- Edit terraform.tfvars with appropriate variables.
2829
- Run the following commands:
2930

3031
```shell
31-
cd transformer/exporter
32+
cd transformer/terraform-cloud
3233
terraform init
3334
terraform apply -auto-approve -var-file=terraform.tfvars
3435
```
3536

3637
A new `export` folder should have been created. The `sg-payload.json` file contains the definition for each workflow that will be created for each Terraform Workspace, and the `states` folder contains the files for the Terraform state for each of your workspaces, if the state export was enabled.
3738

3839
After completing the export , edit the `sg-payload.json` file to provide tune each workflow configuration with the following:
39-
### Use the example_payload.jsonc file to refrence and edit the schema of the `sg-payload.json`
40-
- `DeploymentPlatformConfig` - (Used to authenticate against a cloud provider using a StackGuardian Integration), Create the relevant integration in StackGuardian platform and update `DeploymentPlatformConfig.kind` from the following "AZURE_STATIC", "AWS_STATIC","GCP_STATIC", "AWS_RBAC". Update `DeploymentPlatformConfig.config.integrationId` with "/integrations/INTEGRATION_NAME" and `DeploymentPlatformConfig.config.profileName` with the name of the integration used upon creation.
40+
### Use the example_payload.jsonc file as a reference and edit the schema of the `sg-payload.json`
41+
- `DeploymentPlatformConfig` - THis is used to authenticate against a cloud provider using a StackGuardian Integration. Create the relevant integration in StackGuardian platform and update `DeploymentPlatformConfig.kind` from the following "AZURE_STATIC", "AWS_STATIC","GCP_STATIC", "AWS_RBAC". Update `DeploymentPlatformConfig.config.integrationId` with "/integrations/INTEGRATION_NAME" and `DeploymentPlatformConfig.config.profileName` with the name of the integration used upon creation.
4142
```
4243
DeploymentPlatformConfig: {
4344
"kind": "AWS_RBAC",
@@ -47,33 +48,30 @@ After completing the export , edit the `sg-payload.json` file to provide tune ea
4748
}
4849
}
4950
```
50-
- `VCSConfig` - Provide full path to the `repo` like as well the relevant `sourceConfigDestKind` from the following "GITHUB_COM", "BITBUCKET_ORG", "GITLAB_COM", "AZURE_DEVOPS".
51+
- `VCSConfig` - Provide full path to the `repo` like as well the relevant `sourceConfigDestKind` from the following "GITHUB_COM", "BITBUCKET_ORG", "GITLAB_COM", "AZURE_DEVOPS"
5152
- `config.auth`
5253
- `config.isPrivate`
5354

54-
- `ResourceName` // workspace name
55-
- `wfgrpName` // this corresponds to the labelling of workflow group name in the StackGuardian platform
56-
- `Description` // description for the workflows created in the StackGuardian platform
57-
- `Tags` // list of tags for the workflows created in the StackGuardian platform
58-
- `EnvironmentVariables` // environment variables for the workflows created in the StackGuardian platform
59-
- `RunnerConstraints` // Runner description for the workflows in the StackGuardian platform
60-
- `DeploymentPlatformConfig`
61-
- `Approvers` // Aprrovers for the workflow to run it successfully
62-
- `TerraformConfig` // Terraform configuration for the workflows created in the StackGuardian platform
63-
- `WfType` // this corresponds to the workflow type of the workflow created in the StackGuardian platform
64-
- `UserSchedules` // Scheduled workflow run configuration for the workflow in the StackGuardian platform
65-
- `MiniSteps` // Ministeps for the workflow to direct the process if the workflow returns an error/success/approval required and workflow chaining .
55+
- `ResourceName` - name of your StackGuardian Workflow
56+
- `wfgrpName` - this corresponds to the labelling of workflow group name in the StackGuardian platform
57+
- `Description` - description for the workflows created in the StackGuardian platform
58+
- `Tags` - list of tags for the workflows created in the StackGuardian platform
59+
- `EnvironmentVariables` - environment variables for the workflows created in the StackGuardian platform
60+
- `RunnerConstraints` - Runner description for the workflows in the StackGuardian platform
61+
- `Approvers` - Approvers for the workflow to run it successfully
62+
- `TerraformConfig` - Terraform configuration for the workflows created in the StackGuardian platform
63+
- `UserSchedules` - Scheduled workflow run configuration for the workflow in the StackGuardian platform
64+
- `MiniSteps` - Ministeps for the workflow to direct the process if the workflow returns an error/success/approval required and workflow chaining
6665

6766
### Bulk import workflows to StackGuardian Platform
6867

69-
- Fetch sg-cli (https://github.com/StackGuardian/sg-cli.git) and set up sg-cli locally (documentation present in repo)
68+
- Fetch [sg-cli](https://github.com/StackGuardian/sg-cli.git) and set it up locally (documentation present in repo)
7069
- Run the following commands and pass the `sg-payload.json` as payload (represented below)
70+
- Get your SG API Key here: https://app.stackguardian.io/orchestrator/orgs/<ORG_ID>/settings?tab=api_key
7171

7272
```shell
7373
cd ../../export
7474

75-
Get your SG API Key here: https://app.stackguardian.io/orchestrator/orgs/<org-id>/settings?tab=api_key
76-
7775
export SG_API_TOKEN=<YOUR_SG_API_TOKEN>
7876
wget -q "$(wget -qO- "https://api.github.com/repos/stackguardian/sg-cli/releases/latest" | jq -r '.tarball_url')" -O sg-cli.tar.gz && tar -xf sg-cli.tar.gz && rm -f sg-cli.tar.gz && /bin/cp -rf StackGuardian-sg-cli*/shell/sg-cli . && rm -rfd StackGuardian-sg-cli*
7977

transformer/exporter/locals.tf

Lines changed: 0 additions & 73 deletions
This file was deleted.

transformer/exporter/terraform.tfvars.example

Lines changed: 0 additions & 17 deletions
This file was deleted.
File renamed without changes.

transformer/exporter/data.tf renamed to transformer/terraform-cloud/data.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
data "tfe_workspace_ids" "ids" {
1+
data "tfe_workspace_ids" "data" {
22
names = var.workspaceIds
33
organization = var.tfOrg
44
tag_names = var.tfWorkspaceTags
55
exclude_tags = var.tfWorkspaceIgnoreTags
66
}
77

8-
data "tfe_workspace" "ids" {
8+
data "tfe_workspace" "data" {
99
for_each = toset(local.workflowNames)
1010

1111
name = each.key
1212
organization = var.tfOrg
1313
}
1414

15-
data "tfe_variables" "ids" {
15+
data "tfe_variables" "data" {
1616
for_each = toset(local.workflowIds)
1717

1818
workspace_id = each.key
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
locals {
2+
workflowIds = [for i, v in data.tfe_workspace_ids.data.ids : v]
3+
workflowNames = [for i, v in data.tfe_workspace_ids.data.ids : i]
4+
workflows = [for i, v in data.tfe_workspace_ids.data.ids : {
5+
CLIConfiguration = {
6+
"WorkflowGroup" : {
7+
"name" : data.tfe_workspace.data[i].project_id
8+
},
9+
"TfStateFilePath" : "${abspath(path.root)}/../../${var.exportPath}/states/${data.tfe_workspace.data[i].name}.tfstate"
10+
}
11+
ResourceName = data.tfe_workspace.data[i].name
12+
Description = ""
13+
Tags = data.tfe_workspace.data[i].tag_names
14+
EnvironmentVariables = [for i, v in data.tfe_variables.data[v].variables :
15+
{ "config" : {
16+
"textValue" : v.value,
17+
"varName" : v.name
18+
},
19+
"kind" : "PLAIN_TEXT" } if v.category == "env" && v.sensitive == false]
20+
21+
DeploymentPlatformConfig = []
22+
RunnerConstraints = { "type" : "shared" }
23+
VCSConfig = {
24+
"iacVCSConfig" : {
25+
"useMarketplaceTemplate" : false,
26+
"customSource" : {
27+
"sourceConfigDestKind" : "Choose from: GITHUB_COM, BITBUCKET_ORG, GITLAB_COM, AZURE_DEVOPS",
28+
"config" : {
29+
"includeSubModule" : false,
30+
"ref" : length(data.tfe_workspace.data[i].vcs_repo) > 0 ? data.tfe_workspace.data[i].vcs_repo[0].branch != "" ? data.tfe_workspace.data[i].vcs_repo[0].branch : "" : "",
31+
"isPrivate" : length(data.tfe_workspace.data[i].vcs_repo) > 0 ? length(data.tfe_workspace.data[i].vcs_repo[0].oauth_token_id) > 0 || length(data.tfe_workspace.data[i].vcs_repo[0].github_app_installation_id) > 0 ? true : false : false,
32+
"auth" : length(data.tfe_workspace.data[i].vcs_repo) > 0 ? length(data.tfe_workspace.data[i].vcs_repo[0].oauth_token_id) > 0 || length(data.tfe_workspace.data[i].vcs_repo[0].github_app_installation_id) > 0 ? "Provide an integration id like /integrations/aws-dev-account or /secrets/my-git-token" : "" : "",
33+
"workingDir" : data.tfe_workspace.data[i].working_directory,
34+
"repo" : length(data.tfe_workspace.data[i].vcs_repo) > 0 ? data.tfe_workspace.data[i].vcs_repo[0].identifier : ""
35+
}
36+
}
37+
},
38+
"iacInputData" : {
39+
"schemaType" : "RAW_JSON",
40+
"data" : { for i, v in data.tfe_variables.data[v].variables : v.name => v.value if v.category == "terraform" }
41+
}
42+
}
43+
44+
MiniSteps = {
45+
"wfChaining" : {
46+
"ERRORED" : [],
47+
"COMPLETED" : []
48+
},
49+
"notifications" : {
50+
"email" : {
51+
"ERRORED" : [],
52+
"COMPLETED" : [],
53+
"APPROVAL_REQUIRED" : [],
54+
"CANCELLED" : []
55+
}
56+
}
57+
}
58+
59+
Approvers = data.tfe_workspace.data[i].auto_apply == true ? [] : ["Add emails of the users who should approve the terraform plan, since approvalPreApply is set to true"]
60+
61+
TerraformConfig = {
62+
"managedTerraformState" : true,
63+
"terraformVersion" : data.tfe_workspace.data[i].terraform_version,
64+
"approvalPreApply" : !data.tfe_workspace.data[i].auto_apply
65+
}
66+
67+
WfType = "TERRAFORM"
68+
UserSchedules = []
69+
}]
70+
data = jsonencode(
71+
local.workflows
72+
)
73+
}

transformer/exporter/main.tf renamed to transformer/terraform-cloud/main.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ terraform {
66
source = "hashicorp/local"
77
version = "~> 2.4.0"
88
}
9+
tfe = {
10+
source = "hashicorp/tfe"
11+
version = "~> 0.48.0"
12+
}
913
null = {
1014
source = "hashicorp/null"
1115
version = "~> 3.2.1"
1216
}
13-
tfe = {
14-
source = "hashicorp/tfe"
15-
version = "~> 0.45.0"
16-
}
1717
}
1818
}

transformer/exporter/resources.tf renamed to transformer/terraform-cloud/resources.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ resource "local_file" "data" {
77
}
88

99
resource "local_file" "generateTempTfFiles" {
10-
for_each = var.stateExport ? toset(local.workflowNames) : []
10+
for_each = var.exportStateFiles ? toset(local.workflowNames) : []
1111

1212
content = templatefile("${path.module}/workspace.tmpl", { tfOrg = var.tfOrg, workspace = each.key })
1313
filename = "${path.module}/../../${var.exportPath}/tfDir/${each.key}/main.tf"
@@ -18,7 +18,7 @@ resource "null_resource" "exportStateFiles" {
1818
triggers = {
1919
always-update = timestamp()
2020
}
21-
for_each = var.stateExport ? toset(local.workflowNames) : []
21+
for_each = var.exportStateFiles ? toset(local.workflowNames) : []
2222

2323
provisioner "local-exec" {
2424
command = "mkdir -p ../../states && rm -rf .terraform .terraform.lock.hcl terraform.tfstate terraform.tfstate.backup && terraform init -input=false && terraform state pull > ../../states/'${each.key}.tfstate'"
@@ -27,7 +27,7 @@ resource "null_resource" "exportStateFiles" {
2727
}
2828

2929
resource "null_resource" "deleteTempTfFiles" {
30-
count = var.stateExport ? 1 : 0
30+
count = var.exportStateFiles ? 1 : 0
3131
triggers = {
3232
always-update = timestamp()
3333
}

0 commit comments

Comments
 (0)