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

Update configuration_metadata.go #513

Open
wants to merge 3 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
44 changes: 29 additions & 15 deletions boot/configuration_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"archive/zip"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -135,10 +134,10 @@ func DataFlowConfigurationExists(path string) (bool, error) {

func NewDataFlowConfigurationMetadata(path string, metadata ConfigurationMetadata) (ConfigurationMetadata, error) {
file := filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties")
b, err := ioutil.ReadFile(file)
b, err := os.ReadFile(file)
if os.IsNotExist(err) {
file := filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties")
b, err = ioutil.ReadFile(file)
b, err = os.ReadFile(file)
if os.IsNotExist(err) {
return ConfigurationMetadata{}, nil
} else if err != nil {
Expand All @@ -153,34 +152,49 @@ func NewDataFlowConfigurationMetadata(path string, metadata ConfigurationMetadat
return ConfigurationMetadata{}, fmt.Errorf("unable to load properties from %s\n%w", file, err)
}

// Load classes
s, ok := p.Get("configuration-properties.classes")
if !ok {
return ConfigurationMetadata{}, nil
var classes []string
if ok {
for _, s := range strings.Split(s, ",") {
class := strings.TrimSpace(s)
if class != "" {
classes = append(classes, class)
}
}
}

var classes []string
for _, s := range strings.Split(s, ",") {
class := strings.TrimSpace(s)
if class == "" {
continue
// Load names
s, ok = p.Get("configuration-properties.names")
var names []string
if ok {
for _, s := range strings.Split(s, ",") {
name := strings.TrimSpace(s)
if name != "" {
names = append(names, name)
}
}
classes = append(classes, class)
}

// Merge classes and names
var combined []string
combined = append(combined, classes...)
combined = append(combined, names...)

m := ConfigurationMetadata{}

for _, g := range metadata.Groups {
for _, c := range classes {
if c == g.SourceType {
for _, c := range combined {
if c == g.SourceType || c == g.Name {
m.Groups = append(m.Groups, g)
break
}
}
}

for _, p := range metadata.Properties {
for _, c := range classes {
if c == p.SourceType {
for _, c := range combined {
if c == p.SourceType || c == p.Name {
m.Properties = append(m.Properties, p)
break
}
Expand Down
82 changes: 71 additions & 11 deletions boot/configuration_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package boot_test

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -38,7 +37,7 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {
it.Before(func() {
var err error

path, err = ioutil.TempDir("", "configuration-metadata")
path, err = os.MkdirTemp("", "configuration-metadata")
Expect(err).NotTo(HaveOccurred())
})

Expand All @@ -54,7 +53,7 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {

it("returns decoded contents", func() {
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
[]byte(`{ "groups": [ { "name": "alpha" } ] }`), 0644)).To(Succeed())

Expect(boot.NewConfigurationMetadataFromPath(path)).To(Equal(boot.ConfigurationMetadata{
Expand All @@ -64,9 +63,9 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {

it("returns dataflow decoded contents", func() {
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
[]byte(`{ "groups": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644))
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
[]byte("configuration-properties.classes=alpha"), 0644))

cm, err := boot.NewConfigurationMetadataFromPath(path)
Expand All @@ -79,9 +78,9 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {

it("returns dataflow decoded contents handling trailing comma correctly", func() {
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
[]byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" }, { "name": "beta" } ] }`), 0644))
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
[]byte("configuration-properties.classes=alpha,"), 0644))

cm, err := boot.NewConfigurationMetadataFromPath(path)
Expand All @@ -94,9 +93,9 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {

it("returns dataflow decoded contents", func() {
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
[]byte(`{ "groups": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"),
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"),
[]byte("configuration-properties.classes=alpha"), 0644)).To(Succeed())

cm, err := boot.NewConfigurationMetadataFromPath(path)
Expand All @@ -106,6 +105,67 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {
Groups: []boot.Group{{Name: "alpha", SourceType: "alpha"}},
}))
})

it("returns dataflow decoded contents with names", func() {
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
[]byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)).To(Succeed())
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
[]byte("configuration-properties.names=alpha"), 0644)).To(Succeed())

cm, err := boot.NewConfigurationMetadataFromPath(path)
Expect(err).NotTo(HaveOccurred())

Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{
Properties: []boot.Property{{Name: "alpha", SourceType: "alpha"}},
}))
})

it("returns combined dataflow decoded contents with classes and names", func() {
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
[]byte(`{ "groups": [ { "name": "alpha", "sourceType": "alpha" }, { "name": "beta", "sourceType": "beta" } ] }`), 0644)).To(Succeed())
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
[]byte("configuration-properties.classes=alpha\nconfiguration-properties.names=beta"), 0644)).To(Succeed())

cm, err := boot.NewConfigurationMetadataFromPath(path)
Expect(err).NotTo(HaveOccurred())

Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{
Groups: []boot.Group{
{Name: "alpha", SourceType: "alpha"},
{Name: "beta", SourceType: "beta"},
},
}))
})

it("handles missing names properly", func() {
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
[]byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" }, { "name": "beta", "sourceType": "beta" } ] }`), 0644)).To(Succeed())
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
[]byte("configuration-properties.names=alpha"), 0644)).To(Succeed())

cm, err := boot.NewConfigurationMetadataFromPath(path)
Expect(err).NotTo(HaveOccurred())

Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{
Properties: []boot.Property{{Name: "alpha", SourceType: "alpha"}},
}))
})

it("handles empty names gracefully", func() {
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
[]byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)).To(Succeed())
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
[]byte("configuration-properties.names="), 0644)).To(Succeed())

cm, err := boot.NewConfigurationMetadataFromPath(path)
Expect(err).NotTo(HaveOccurred())

Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{}))
})
})

context("from JAR", func() {
Expand All @@ -132,14 +192,14 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {

it("returns true if the file does exist", func() {
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"),
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"),
[]byte("configuration-properties.classes=alpha"), 0644)).To(Succeed())
Expect(boot.DataFlowConfigurationExists(path)).To(BeFalse())
})

it("return false and the error if the file cannot be read", func() {
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"),
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"),
[]byte("configuration-properties.classes=alpha"), 0644)).To(Succeed())

Expect(os.Chmod(filepath.Join(path, "META-INF"), 0000)).To(Succeed())
Expand Down
Loading