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

feat: improve variable handling #61

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 14 additions & 7 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import (
"gopkg.in/yaml.v3"
)

type ErrUndefinedPath struct {
Path []interface{}
FuncName string
}

func (e ErrUndefinedPath) Error() string {
return fmt.Sprintf("%s: undefined path: %v", e.FuncName, e.Path)
}

// Data is an arbitrary map of values which makes up the inventory.
type Data map[string]interface{}

Expand Down Expand Up @@ -79,14 +88,14 @@ func (d Data) GetPath(path ...interface{}) (tree interface{}, err error) {
}
tree, ok = node[key]
if !ok {
return nil, fmt.Errorf("key not found: %v", el)
return nil, ErrUndefinedPath{Path: path, FuncName: "GetPath"}
}

case map[interface{}]interface{}:
var ok bool
tree, ok = node[el]
if !ok {
return nil, fmt.Errorf("key not found: %v", el)
return nil, ErrUndefinedPath{Path: path, FuncName: "GetPath"}
}

case []interface{}:
Expand All @@ -101,9 +110,8 @@ func (d Data) GetPath(path ...interface{}) (tree interface{}, err error) {
return nil, fmt.Errorf("path index out of range: %d", index)
}
tree = node[index]

default:
return nil, fmt.Errorf("unexpected node type %T at index %d", node, i)
return nil, ErrUndefinedPath{Path: path, FuncName: "GetPath"}
}
}
return tree, nil
Expand Down Expand Up @@ -151,9 +159,8 @@ func (d *Data) SetPath(value interface{}, path ...interface{}) (err error) {
return fmt.Errorf("path index out of range: %d", index)
}
node[index] = value

default:
return fmt.Errorf("unexpected node type %T at index %d", node, i)
case string:
return ErrUndefinedPath{Path: path, FuncName: "SetPath"}
}

return nil
Expand Down
2 changes: 2 additions & 0 deletions examples/secrets/compiled/azure_keyvault/AzureReadme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Azure KeyVault

120 changes: 120 additions & 0 deletions examples/secrets/compiled/azure_keyvault/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,122 @@
# Azure KeyVault

azure_keyvault
INVALID DEFAULT VALUE
defined
{
"alphaNum": "test",
"azure": {
"common": {
"baz": null,
"baz2": null,
"foo": {
"bar": "test"
},
"from_target": "test_CHANGED",
"self_reference": "bar",
"skipper": {
"copy": [
{
"source": "inventory.json",
"target": "something_else/foobar.json"
}
]
},
"subscription_id": "INVALID DEFAULT VALUE",
"this": {
"complex": "object",
"is": "a",
"which": [
"I",
"WANT",
"TO",
"INCLUDE"
]
}
},
"resources": {
"location": "westeurope",
"resource_group": {
"name": "rg-azure_keyvault-terraform-example-westeurope"
},
"vnet": {
"address_space": [
"10.1.0.0/16",
"10.2.0.0/16"
],
"name": "vnet-azure_keyvault-terraform-example",
"subnets": {
"virtual_machines": {
"address_prefixes": [
"10.1.1.0/24"
],
"name": "virtual_machines"
}
}
}
}
},
"features": {
"production": {
"feature1": false,
"feature2": false
},
"sandbox": {
"asdf": "asdf",
"feature1": true,
"feature2": true
}
},
"foo": "defined",
"import": {
"complex": "object",
"is": "a",
"which": [
"I",
"WANT",
"TO",
"INCLUDE"
]
},
"secrets": {
"test1": "?{azurekv:targets/azure_keyvault/test1||randomstring:32}",
"test2": "?{azurekv:targets/azure_keyvault/test2||randomstring:64}",
"test3": "?{azurekv:targets/azure_keyvault/test2}"
},
"skipper": {
"components": [
{
"input_paths": [
"AzureReadme.md",
"inventory.json"
],
"output_path": ".",
"rename": [
{
"filename": "README.md",
"input_path": "AzureReadme.md"
}
]
}
],
"copy": [
{
"source": "inventory.json",
"target": "something/foobar.json"
}
],
"secrets": {
"drivers": {
"azurekv": {
"ignore_version": true,
"key_id": "https://kv-dev-infra-platform.vault.azure.net/keys/dev-infra-secrets-key/6e0360a098eb4808af5ec1f970d399c0"
}
}
},
"use": [
"azure.*",
"features"
]
},
"test": "azure_keyvault"
}
42 changes: 38 additions & 4 deletions examples/secrets/compiled/azure_keyvault/inventory.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
"alphaNum": "test",
"azure": {
"common": {
"baz": "test",
"baz2": "test",
"baz": null,
"baz2": null,
"foo": {
"bar": "test"
},
"from_target": "test_CHANGED",
"self_reference": "bar",
"skipper": {
"copy": [
{
Expand All @@ -16,7 +17,17 @@
}
]
},
"subscription_id": "INVALID DEFAULT VALUE"
"subscription_id": "INVALID DEFAULT VALUE",
"this": {
"complex": "object",
"is": "a",
"which": [
"I",
"WANT",
"TO",
"INCLUDE"
]
}
},
"resources": {
"location": "westeurope",
Expand All @@ -40,6 +51,28 @@
}
}
},
"features": {
"production": {
"feature1": false,
"feature2": false
},
"sandbox": {
"asdf": "asdf",
"feature1": true,
"feature2": true
}
},
"foo": "defined",
"import": {
"complex": "object",
"is": "a",
"which": [
"I",
"WANT",
"TO",
"INCLUDE"
]
},
"secrets": {
"test1": "?{azurekv:targets/azure_keyvault/test1||randomstring:32}",
"test2": "?{azurekv:targets/azure_keyvault/test2||randomstring:64}",
Expand Down Expand Up @@ -76,7 +109,8 @@
}
},
"use": [
"azure.*"
"azure.*",
"features"
]
},
"test": "azure_keyvault"
Expand Down
2 changes: 2 additions & 0 deletions examples/secrets/compiled/test/AzureReadme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Azure KeyVault

8 changes: 8 additions & 0 deletions examples/secrets/inventory/classes/features.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
features:
sandbox:
feature1: true
feature2: true
asdf: "asdf"
production:
feature1: false
feature2: false
12 changes: 12 additions & 0 deletions examples/secrets/inventory/targets/azure_keyvault.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ target:
skipper:
use:
- azure.*
- features
components:
- output_path: .
input_paths:
Expand All @@ -20,6 +21,16 @@ target:
ignore_version: true
key_id: "https://kv-dev-infra-platform.vault.azure.net/keys/dev-infra-secrets-key/6e0360a098eb4808af5ec1f970d399c0"

features:
sandbox: ${features:sandbox}

asdf: ${asdf}

#features: ${features:production}

azure:
common:
self_reference: "bar"
secrets:
test1: ?{azurekv:targets/${target_name}/test1||randomstring:32}
test2: ?{azurekv:targets/${target_name}/test2||randomstring:64}
Expand All @@ -29,3 +40,4 @@ target:
alphaNum: "%{loweralpha:${azure:common:foo:bar}}"

import: ${azure:common:this}
foo: "defined"
13 changes: 2 additions & 11 deletions examples/secrets/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"log"
"path"

"github.com/lukasjarosch/skipper"
Expand Down Expand Up @@ -32,29 +31,21 @@ func main() {
panic(err)
}

log.Printf("\n%s", data.String())
// log.Printf("\n%s", data.String())

templateOutputPath := path.Join(outputPath, target)
templater, err := skipper.NewTemplater(fileSystem, templatePath, templateOutputPath, nil)
if err != nil {
panic(err)
}

templateData := struct {
Inventory any
TargetName string
}{
Inventory: data,
TargetName: target,
}

skipperConfig, err := inventory.GetSkipperConfig(target)
if err != nil {
panic(err)
}

// execute templates ----------------------------------------------------------------------------------
err = templater.ExecuteComponents(templateData, skipperConfig.Components, false)
err = templater.ExecuteComponents(skipper.DefaultTemplateContext(data, target), skipperConfig.Components, false)
if err != nil {
panic(err)
}
Expand Down
4 changes: 4 additions & 0 deletions examples/secrets/templates/AzureReadme.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# Azure KeyVault

{{ .TargetName }}
{{ .Inventory.azure.common.subscription_id }}
{{ .Inventory.foo }}
{{ .Inventory | toPrettyJson }}
Loading