Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Rain::Env for pkg command #140

Merged
merged 2 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cft/pkg/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// or as one-property objects, much as AWS instrinsic functions are used, e.g. "Fn::Join"
//
// `Rain::Include`: insert the content of the file into the template directly. The file must be in YAML or JSON format.
// `Rain::Env`: inserts environmental variable value into the template as a string. Variable must be set.
// `Rain::Embed`: insert the content of the file as a string
// `Rain::S3Http`: uploads the file or directory (zipping it first) to S3 and returns the HTTP URI (i.e. `https://bucket.s3.region.amazonaws.com/key`)
// `Rain::S3`: a string value uploads the file or directory (zipping it first) to S3 and returns the S3 URI (i.e. `s3://bucket/key`)
Expand Down
20 changes: 19 additions & 1 deletion cft/pkg/pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,28 @@ func TestInclude(t *testing.T) {
"Rain::Include": fileName,
},
})

compare(t, in, "Test", map[string]interface{}{"This": "is a test"})
}

func TestEnv(t *testing.T) {
os.Setenv("RAIN_TEST_ENV_EXISTS", "foo")
in1, _ := parse.Map(map[string]interface{}{
"Success": map[string]interface{}{
"Rain::Env": "RAIN_TEST_ENV_EXISTS",
},
})
in2, _ := parse.Map(map[string]interface{}{
"Failure": map[string]interface{}{
"Rain::Env": "RAIN_TEST_ENV_DOESNT_EXISTS",
},
})
compare(t, in1, "Success", "foo")
_, err2 := pkg.Template(in2, "./")
if err2 == nil {
t.Errorf("Expected error since %q environment variable doesn't exist", "RAIN_TEST_ENV_DOESNT_EXISTS")
}
}

func TestS3Http(t *testing.T) {
in, _ := parse.Map(map[string]interface{}{
"Test": map[string]interface{}{
Expand Down
20 changes: 20 additions & 0 deletions cft/pkg/rain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package pkg

import (
"os"
"errors"
"fmt"
"path/filepath"
Expand Down Expand Up @@ -37,6 +38,7 @@ var registry = make(map[string]rainFunc)
func init() {
registry["**/*|Rain::Embed"] = includeString
registry["**/*|Rain::Include"] = includeLiteral
registry["**/*|Rain::Env"] = includeEnv
registry["**/*|Rain::S3Http"] = includeS3Http
registry["**/*|Rain::S3"] = includeS3
registry["**/*|Rain::Module"] = module
Expand Down Expand Up @@ -78,6 +80,24 @@ func includeLiteral(n *yaml.Node, root string, t cft.Template, parent node.NodeP
return true, nil
}

func includeEnv(n *yaml.Node, root string, t cft.Template, parent node.NodePair) (bool, error) {
name, err := expectString(n)
if err != nil {
return false, err
}
val, present := os.LookupEnv( name )
if !present {
return false, fmt.Errorf("missing environmental variable %q", name)
}
var newNode yaml.Node
newNode.Encode ( val )
if err != nil {
return false, err
}
*n = newNode
return true, nil
}

func handleS3(root string, options s3Options) (*yaml.Node, error) {
s, err := upload(root, options.Path, options.Zip)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cft/pkg/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// or as one-property objects, much as AWS instrinsic functions are used, e.g. "Fn::Join"
//
// `Rain::Include`: insert the content of the file into the template directly. The file must be in YAML or JSON format.
// `Rain::Env`: inserts environmental variable value into the template as a string. Variable must be set.
// `Rain::Embed`: insert the content of the file as a string
// `Rain::S3Http`: uploads the file or directory (zipping it first) to S3 and returns the HTTP URI (i.e. `https://bucket.s3.region.amazonaws.com/key`)
// `Rain::S3`: a string value uploads the file or directory (zipping it first) to S3 and returns the S3 URI (i.e. `s3://bucket/key`)
Expand Down
1 change: 1 addition & 0 deletions cft/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var Tags = map[string]string{
"!Condition": "Condition",
"!Rain::Embed": "Rain::Embed",
"!Rain::Include": "Rain::Include",
"!Rain::Env": "Rain::Env",
"!Rain::S3Http": "Rain::S3Http",
"!Rain::S3": "Rain::S3",
"!Rain::Module": "Rain::Module",
Expand Down
2 changes: 2 additions & 0 deletions docs/rain_pkg.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ You may use the following, rain-specific directives in templates packaged with "

!Rain::Include <path> Reads the file at <path> as YAML/JSON and inserts the resulting object into the template

!Rain::Env <name> Reads the <name> environmental variable and inserts value into the template as a string

!Rain::S3Http <path> Uploads <path> (zipping first if it is a directory) to S3
and embeds the S3 HTTP URL into the template as a string

Expand Down
4 changes: 2 additions & 2 deletions internal/aws/s3/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package s3
import (
"crypto/sha256"
"fmt"
"path/filepath"

"github.com/aws-cloudformation/rain/internal/aws"
"github.com/aws-cloudformation/rain/internal/config"
Expand Down Expand Up @@ -32,8 +33,7 @@ func Upload(bucketName string, content []byte) (string, error) {
if !isBucketExists {
return "", fmt.Errorf("bucket does not exist: '%s'", bucketName)
}

return fmt.Sprintf("%x", sha256.Sum256(content)), nil
return filepath.Join ( BucketKeyPrefix, fmt.Sprintf("%x", sha256.Sum256(content)) ), nil
}

// RainBucket returns the name of the rain deployment bucket in the current region
Expand Down
47 changes: 47 additions & 0 deletions internal/aws/s3/s3_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//go:build func_test

package s3

import (
"fmt"
"testing"

"github.com/aws-cloudformation/rain/internal/aws"
)

func TestPassingBucketName(t *testing.T) {
tests := [][]string {
{"custom-bucket-name", "custom-bucket-name"},
{"", fmt.Sprintf("rain-artifacts-1234567890-%s", aws.Config().Region) },
}
for _, test := range tests {
BucketName = test[0]
output := RainBucket(false)
if output != test[1] {
t.Errorf("incorrect bucket name, expecting %q but got %q", test[1], output)
}
}
}

func TestPassingBucketKeyPrefix(t *testing.T) {
buckets ["random-bucket"] = true
content := []byte("some content")
hash := "290f493c44f5d63d06b374d0a5abd292fae38b92cab2fae5efefe1b0e9347f56"
tests := [][]string {
{"some-prefix", fmt.Sprintf ("some-prefix/%s", hash)},
{"some-prefix/", fmt.Sprintf ("some-prefix/%s", hash)},
{"some-prefix/test", fmt.Sprintf ("some-prefix/test/%s", hash)},
{"some-prefix/test/", fmt.Sprintf ("some-prefix/test/%s", hash)},
{"", hash},
}
for _, test := range tests {
BucketKeyPrefix = test[0]
output, err := Upload("random-bucket", content)
if err != nil {
t.Errorf("unexpected error during upload: %v", err)
}
if output != test[1] {
t.Errorf("incorrect key, expecting %q but got %q", test[1], output)
}
}
}
2 changes: 2 additions & 0 deletions internal/cmd/pkg/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ You may use the following, rain-specific directives in templates packaged with "

!Rain::Include <path> Reads the file at <path> as YAML/JSON and inserts the resulting object into the template

!Rain::Env <name> Reads the <name> environmental variable and inserts value into the template as a string

!Rain::S3Http <path> Uploads <path> (zipping first if it is a directory) to S3
and embeds the S3 HTTP URL into the template as a string

Expand Down