Skip to content

Commit

Permalink
Read environment vars from SSM (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
callumevans authored Dec 3, 2024
1 parent 9b10f55 commit 80bb05c
Show file tree
Hide file tree
Showing 26 changed files with 230 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go-releaser-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

jobs:
go-releaser-checks:
timeout-minutes: 3
timeout-minutes: 5
runs-on: ubuntu-latest
steps:
- name: Check out repository
Expand Down
17 changes: 6 additions & 11 deletions .github/workflows/offline-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,20 @@ jobs:
run: |-
bash ./_start.sh &
- name: Wait for API to be ready
run: |
timeout 60s bash -c 'while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' 127.0.0.1:8081)" != "200" ]]; do sleep 2; done' || false
- name: Install Hurl
run: |
VERSION=5.0.1
curl --location --remote-name https://github.com/Orange-OpenSource/hurl/releases/download/$VERSION/hurl_${VERSION}_amd64.deb
sudo apt update && sudo apt install ./hurl_${VERSION}_amd64.deb
- name: Run Parallel Hurl tests
- name: Wait for API to be ready
run: |
hurl --test --glob *.hurl
working-directory: ./tests/parallel

- name: Run Sequential Hurl tests
timeout 60s bash -c 'while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' 127.0.0.1:8081)" != "200" ]]; do sleep 2; done' || false
- name: Run Hurl tests
run: |
hurl --test --glob *.hurl
working-directory: ./tests/serial
working-directory: ./tests/requests

- name: Stop API
run: pkill terrable
Expand Down
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
__debug_bin**.exe
terrable.exe
samples/*/.terraform.lock.hcl
samples/*/.terraform/*
samples/**/*terraform.tfstate*
/samples/**/*-package.zip
.terrable/
dist/
__debug_bin*
__debug_bin*

terrable.exe
terrable
tests/terrable
10 changes: 6 additions & 4 deletions config/terrable_config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package config

type TerrableConfig struct {
Handlers []HandlerMapping
Handlers []HandlerMapping
GlobalEnvironmentVariables map[string]string
}

type HandlerMapping struct {
Name string
Source string
Http map[string]string
Name string
Source string
Http map[string]string
EnvironmentVariables map[string]string
}
3 changes: 1 addition & 2 deletions config/terrable_toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
)

type TerrableToml struct {
Environment map[string]string `toml:"environment"`
Offline OfflineConfig `toml:"offline"`
Offline OfflineConfig `toml:"offline"`
}

type OfflineConfig struct {
Expand Down
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,22 @@ require (
require (
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/aws/aws-sdk-go-v2 v1.32.6 // indirect
github.com/aws/aws-sdk-go-v2/config v1.28.6 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.47 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.21 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.25 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.25 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.1 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.6 // indirect
github.com/aws/aws-sdk-go-v2/service/ssm v1.56.1 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.24.7 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.6 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.33.2 // indirect
github.com/aws/smithy-go v1.22.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
Expand Down
36 changes: 36 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,37 @@ github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7l
github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY=
github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4=
github.com/aws/aws-sdk-go-v2 v1.32.6 h1:7BokKRgRPuGmKkFMhEg/jSul+tB9VvXhcViILtfG8b4=
github.com/aws/aws-sdk-go-v2 v1.32.6/go.mod h1:P5WJBrYqqbWVaOxgH0X/FYYD47/nooaPOZPlQdmiN2U=
github.com/aws/aws-sdk-go-v2/config v1.28.6 h1:D89IKtGrs/I3QXOLNTH93NJYtDhm8SYa9Q5CsPShmyo=
github.com/aws/aws-sdk-go-v2/config v1.28.6/go.mod h1:GDzxJ5wyyFSCoLkS+UhGB0dArhb9mI+Co4dHtoTxbko=
github.com/aws/aws-sdk-go-v2/credentials v1.17.47 h1:48bA+3/fCdi2yAwVt+3COvmatZ6jUDNkDTIsqDiMUdw=
github.com/aws/aws-sdk-go-v2/credentials v1.17.47/go.mod h1:+KdckOejLW3Ks3b0E3b5rHsr2f9yuORBum0WPnE5o5w=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.21 h1:AmoU1pziydclFT/xRV+xXE/Vb8fttJCLRPv8oAkprc0=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.21/go.mod h1:AjUdLYe4Tgs6kpH4Bv7uMZo7pottoyHMn4eTcIcneaY=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.25 h1:s/fF4+yDQDoElYhfIVvSNyeCydfbuTKzhxSXDXCPasU=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.25/go.mod h1:IgPfDv5jqFIzQSNbUEMoitNooSMXjRSDkhXv8jiROvU=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.25 h1:ZntTCl5EsYnhN/IygQEUugpdwbhdkom9uHcbCftiGgA=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.25/go.mod h1:DBdPrgeocww+CSl1C8cEV8PN1mHMBhuCDLpXezyvWkE=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 h1:VaRN3TlFdd6KxX1x3ILT5ynH6HvKgqdiXoTxAF4HQcQ=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1/go.mod h1:FbtygfRFze9usAadmnGJNc8KsP346kEe+y2/oyhGAGc=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.1 h1:iXtILhvDxB6kPvEXgsDhGaZCSC6LQET5ZHSdJozeI0Y=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.1/go.mod h1:9nu0fVANtYiAePIBh2/pFUSwtJ402hLnp854CNoDOeE=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.6 h1:50+XsN70RS7dwJ2CkVNXzj7U2L1HKP8nqTd3XWEXBN4=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.6/go.mod h1:WqgLmwY7so32kG01zD8CPTJWVWM+TzJoOVHwTg4aPug=
github.com/aws/aws-sdk-go-v2/service/ssm v1.56.1 h1:cfVjoEwOMOJOI6VoRQua0nI0KjZV9EAnR8bKaMeSppE=
github.com/aws/aws-sdk-go-v2/service/ssm v1.56.1/go.mod h1:fGHwAnTdNrLKhgl+UEeq9uEL4n3Ng4MJucA+7Xi3sC4=
github.com/aws/aws-sdk-go-v2/service/sso v1.24.7 h1:rLnYAfXQ3YAccocshIH5mzNNwZBkBo+bP6EhIxak6Hw=
github.com/aws/aws-sdk-go-v2/service/sso v1.24.7/go.mod h1:ZHtuQJ6t9A/+YDuxOLnbryAmITtr8UysSny3qcyvJTc=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.6 h1:JnhTZR3PiYDNKlXy50/pNeix9aGMo6lLpXwJ1mw8MD4=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.6/go.mod h1:URronUEGfXZN1VpdktPSD1EkAL9mfrV+2F4sjH38qOY=
github.com/aws/aws-sdk-go-v2/service/sts v1.33.2 h1:s4074ZO1Hk8qv65GqNXqDjmkf4HSQqJukaLuuW0TpDA=
github.com/aws/aws-sdk-go-v2/service/sts v1.33.2/go.mod h1:mVggCnIWoM09jP71Wh+ea7+5gAp53q+49wDFs1SW5z8=
github.com/aws/smithy-go v1.22.1 h1:/HPHZQ0g7f4eUeK6HKglFz8uwVfZKgoI25rb/J+dnro=
github.com/aws/smithy-go v1.22.1/go.mod h1:irrKGvNn1InZwb2d7fkIRNucdfwR8R+Ts3wxYa/cJHg=
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/evanw/esbuild v0.23.0 h1:PLUwTn2pzQfIBRrMKcD3M0g1ALOKIHMDefdFCk7avwM=
Expand All @@ -20,10 +49,15 @@ github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/hashicorp/hcl/v2 v2.21.0 h1:lve4q/o/2rqwYOgUg3y3V2YPyD1/zkCLGjIV74Jit14=
github.com/hashicorp/hcl/v2 v2.21.0/go.mod h1:62ZYHrXgPoX8xBnzl8QzbWq4dyDsDtfCRgIq1rbJEvA=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/urfave/cli/v2 v2.27.3 h1:/POWahRmdh7uztQ3CYnaDddk0Rm90PyOgIxgW2rr41M=
github.com/urfave/cli/v2 v2.27.3/go.mod h1:m4QzxcD2qpra4z7WhzEGn74WZLViBnMpb1ToCAKdGRQ=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
Expand All @@ -43,3 +77,5 @@ golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
1 change: 0 additions & 1 deletion offline/handler_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ type HandlerInstance struct {
handlerTranspiledPath string
readCodeMutex sync.RWMutex
recompileSyncLock *sync.Once
executionMutex sync.Mutex
envVars map[string]string
}

Expand Down
8 changes: 6 additions & 2 deletions offline/handler_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import (
"net/http"
"os"
"strings"
"sync"

"github.com/gorilla/mux"
)

var handlerExecutionMutex sync.Mutex

func ServeHandler(handlerInstance *HandlerInstance, r *mux.Router) {
inputFiles := handlerInstance.CompileHandler()
go handlerInstance.WatchForChanges(inputFiles)
Expand All @@ -28,8 +31,8 @@ func ServeHandler(handlerInstance *HandlerInstance, r *mux.Router) {
// TODO: Emulate API Gateway's 404 for missing routes / methods

go r.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
handlerInstance.executionMutex.Lock()
defer handlerInstance.executionMutex.Unlock()
handlerExecutionMutex.Lock()
defer handlerExecutionMutex.Unlock()

code := generateHandlerRuntimeCode(handlerInstance, r)

Expand Down Expand Up @@ -172,6 +175,7 @@ func generateHandlerRuntimeCode(handler *HandlerInstance, r *http.Request) strin

return fmt.Sprintf(`
const env = %s;
process.env = {};
for (const envKey in env) {
process.env[envKey] = env[envKey];
Expand Down
18 changes: 15 additions & 3 deletions offline/offline.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ func Run(filePath string, moduleName string, port string) error {

// TODO: Validate config

tomlConfig, err := config.ParseTerrableToml()

if err != nil {
panic(fmt.Errorf("error parsing .terrable.toml file: %w", err))
}
Expand Down Expand Up @@ -52,7 +50,7 @@ func Run(filePath string, moduleName string, port string) error {

ServeHandler(&HandlerInstance{
handlerConfig: handler,
envVars: tomlConfig.Environment,
envVars: mergeEnvMaps(terrableConfig.GlobalEnvironmentVariables, handler.EnvironmentVariables),
}, r)
}(handler)
}
Expand Down Expand Up @@ -109,3 +107,17 @@ func printConfig(config config.TerrableConfig, port int) {
fmt.Printf("%d Endpoint(s) to prepare...\n", totalEndpoints)
fmt.Print(strings.Join(printlines, ""))
}

func mergeEnvMaps(global, local map[string]string) map[string]string {
merged := make(map[string]string, len(global)+len(local))

for k, v := range global {
merged[k] = v
}

for k, v := range local {
merged[k] = v
}

return merged
}
14 changes: 14 additions & 0 deletions samples/simple/simple-api.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,29 @@ module "simple_api" {
source = "terrable-dev/terrable-api/aws"
version = "0.0.4"
api_name = "simple-api"

global_environment_variables = {
GLOBAL_ENV = "global-env-var"
}

handlers = {
EchoHandler: {
source = "./src/Echo.ts"
environment_variables = {
ECHO_ENV = "echo-env"
}
http = {
GET = "/",
POST = "/"
}
},
# Echo Handler with no local environment variables
EchoHandlerNoLocalEnv: {
source = "./src/Echo.ts"
http = {
GET = "/echo-no-env",
}
},
DelayedHandler: {
source = "./src/Delayed.ts"
http = {
Expand Down
2 changes: 1 addition & 1 deletion terrable_build
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = 0.1.9
version = 0.2.0
9 changes: 9 additions & 0 deletions terraform.tfstate
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": 4,
"terraform_version": "1.9.8",
"serial": 1,
"lineage": "3d39f946-6e89-d2de-0b1b-9ae70363b2e7",
"outputs": {},
"resources": [],
"check_results": null
}
3 changes: 0 additions & 3 deletions tests/.terrable.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[environment]
TOML_VAR = "hello toml"

[offline]
file = "../samples/simple/simple-api.tf"
module = "simple_api"
Expand Down
5 changes: 0 additions & 5 deletions tests/parallel/env_vars.hurl

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions tests/requests/env_vars.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
GET http://127.0.0.1:8081/
HTTP 200

[Asserts]
jsonpath "$.env.GLOBAL_ENV" == "global-env-var"
jsonpath "$.env.ECHO_ENV" == "echo-env"

GET http://127.0.0.1:8081/echo-no-env
HTTP 200

[Asserts]
jsonpath "$.env.GLOBAL_ENV" == "global-env-var"
jsonpath "$.env.ECHO_ENV" not exists

File renamed without changes.
File renamed without changes.
Binary file removed tests/terrable
Binary file not shown.
41 changes: 41 additions & 0 deletions utils/ssm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package utils

import (
"context"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ssm"
)

func initSSMClient() (*ssm.Client, error) {
cfg, err := config.LoadDefaultConfig(context.Background())

if err != nil {
return nil, err
}

ssmClient := ssm.NewFromConfig(cfg)

return ssmClient, nil
}

func FetchSSMParameter(parameterName string) (string, error) {
ssmClient, err := initSSMClient()
if err != nil {
return "", err
}

input := &ssm.GetParameterInput{
Name: aws.String(parameterName),
WithDecryption: aws.Bool(true),
}

result, err := ssmClient.GetParameter(context.Background(), input)

if err != nil {
return "", err
}

return *result.Parameter.Value, nil
}
Loading

0 comments on commit 80bb05c

Please sign in to comment.