Skip to content

Commit 1e2a950

Browse files
authored
Merge pull request #1167 from kiril-keranov/patch-15
[go-migration] Adjust missing framework configurations
2 parents 116640e + e740bdb commit 1e2a950

16 files changed

Lines changed: 631 additions & 245 deletions

src/integration/frameworks_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ func testFrameworks(platform switchblade.Platform, fixtures string) func(*testin
536536
deployment, logs, err := platform.Deploy.
537537
WithEnv(map[string]string{
538538
"BP_JAVA_VERSION": "11",
539-
"JBP_CONFIG_CLIENT_CERTIFICATE_MAPPER": "'{enabled: true}'",
539+
"JBP_CONFIG_CLIENT_CERTIFICATE_MAPPER": "{ enabled: true }",
540540
}).
541541
Execute(name, filepath.Join(fixtures, "containers", "tomcat"))
542542
Expect(err).NotTo(HaveOccurred(), logs.String)
@@ -550,7 +550,7 @@ func testFrameworks(platform switchblade.Platform, fixtures string) func(*testin
550550
deployment, logs, err := platform.Deploy.
551551
WithEnv(map[string]string{
552552
"BP_JAVA_VERSION": "11",
553-
"JBP_CONFIG_CLIENT_CERTIFICATE_MAPPER": "'{enabled: false}'",
553+
"JBP_CONFIG_CLIENT_CERTIFICATE_MAPPER": "{ enabled: false }",
554554
}).
555555
Execute(name, filepath.Join(fixtures, "containers", "tomcat"))
556556
Expect(err).NotTo(HaveOccurred(), logs.String)
@@ -851,7 +851,7 @@ func testFrameworks(platform switchblade.Platform, fixtures string) func(*testin
851851
deployment, logs, err := platform.Deploy.
852852
WithEnv(map[string]string{
853853
"BP_JAVA_VERSION": "11",
854-
"JBP_CONFIG_JPROFILER_PROFILER": "'{enabled: true}'",
854+
"JBP_CONFIG_JPROFILER_PROFILER": "{ enabled: true }",
855855
}).
856856
Execute(name, filepath.Join(fixtures, "containers", "spring_boot_staged"))
857857
Expect(err).NotTo(HaveOccurred(), logs.String)

src/java/common/context.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ package common
33
import (
44
"encoding/json"
55
"fmt"
6+
"github.com/cloudfoundry/libbuildpack"
67
"os"
78
"path/filepath"
89
"strconv"
910
"strings"
10-
11-
"github.com/cloudfoundry/libbuildpack"
1211
)
1312

1413
// Context holds shared dependencies for buildpack components

src/java/common/yaml_handler.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package common
2+
3+
import (
4+
"bytes"
5+
"go.yaml.in/yaml/v3"
6+
)
7+
8+
// YamlHandler provides a thin wrapper around yaml.v3's Marshal and Unmarshal.
9+
type YamlHandler struct{}
10+
11+
// Unmarshal decodes the YAML data into the provided destination.
12+
func (h YamlHandler) Unmarshal(data []byte, out any) error {
13+
return yaml.Unmarshal(data, out)
14+
}
15+
16+
// Marshal encodes the given value into YAML.
17+
func (h YamlHandler) Marshal(in any) ([]byte, error) {
18+
return yaml.Marshal(in)
19+
}
20+
21+
// ValidateFields is used to detect unknown fields during parsing of JBP_CONFIG* configurations
22+
func (h YamlHandler) ValidateFields(data []byte, out interface{}) error {
23+
dec := yaml.NewDecoder(bytes.NewReader(data))
24+
dec.KnownFields(true)
25+
return dec.Decode(out)
26+
}

src/java/frameworks/aspectj_weaver_agent.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package frameworks
22

33
import (
4-
"github.com/cloudfoundry/java-buildpack/src/java/common"
54
"fmt"
5+
"github.com/cloudfoundry/java-buildpack/src/java/common"
66
"os"
77
"path/filepath"
88
"strings"
@@ -22,6 +22,15 @@ func NewAspectJWeaverAgentFramework(ctx *common.Context) *AspectJWeaverAgentFram
2222

2323
// Detect determines if AspectJ Weaver JAR and configuration exist in the application
2424
func (a *AspectJWeaverAgentFramework) Detect() (string, error) {
25+
config, err := a.loadConfig()
26+
if err != nil {
27+
a.context.Log.Warning("Failed to load aspectj weaver agent config: %s", err.Error())
28+
return "", nil // Don't fail the build
29+
}
30+
31+
if !config.isEnabled() {
32+
return "", nil
33+
}
2534
// Look for aspectjweaver-*.jar in the application
2635
aspectjJar, err := a.findAspectJWeaver()
2736
if err != nil || aspectjJar == "" {
@@ -127,3 +136,32 @@ func (a *AspectJWeaverAgentFramework) findAspectJWeaver() (string, error) {
127136

128137
return "", nil
129138
}
139+
140+
func (a *AspectJWeaverAgentFramework) loadConfig() (*aspectjWeaverConfig, error) {
141+
// initialize default values
142+
ajwConfig := aspectjWeaverConfig{
143+
Enabled: true,
144+
}
145+
config := os.Getenv("JBP_CONFIG_ASPECTJ_WEAVER_AGENT")
146+
if config != "" {
147+
yamlHandler := common.YamlHandler{}
148+
err := yamlHandler.ValidateFields([]byte(config), &ajwConfig)
149+
if err != nil {
150+
a.context.Log.Warning("Unknown user config values: %s", err.Error())
151+
}
152+
// overlay JBP_CONFIG_ASPECTJ_WEAVER_AGENT over default values
153+
if err = yamlHandler.Unmarshal([]byte(config), &ajwConfig); err != nil {
154+
return nil, fmt.Errorf("failed to parse JBP_CONFIG_ASPECTJ_WEAVER_AGENT: %w", err)
155+
}
156+
}
157+
return &ajwConfig, nil
158+
}
159+
160+
type aspectjWeaverConfig struct {
161+
Enabled bool `yaml:"enabled"`
162+
}
163+
164+
// isEnabled checks if aspectj weaver agent is enabled
165+
func (a *aspectjWeaverConfig) isEnabled() bool {
166+
return a.Enabled
167+
}

src/java/frameworks/client_certificate_mapper.go

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package frameworks
22

33
import (
4-
"github.com/cloudfoundry/java-buildpack/src/java/common"
54
"fmt"
5+
"github.com/cloudfoundry/java-buildpack/src/java/common"
66
"os"
77
"path/filepath"
88
)
@@ -23,7 +23,13 @@ func NewClientCertificateMapperFramework(ctx *common.Context) *ClientCertificate
2323
// Enabled by default to support mTLS scenarios, can be disabled via configuration
2424
func (c *ClientCertificateMapperFramework) Detect() (string, error) {
2525
// Check if explicitly disabled via configuration
26-
if !c.isEnabled() {
26+
config, err := c.loadConfig()
27+
if err != nil {
28+
c.context.Log.Warning("Failed to load client certificate mapper config: %s", err.Error())
29+
return "", nil // Don't fail the build
30+
}
31+
32+
if !config.isEnabled() {
2733
return "", nil
2834
}
2935

@@ -77,25 +83,31 @@ func (c *ClientCertificateMapperFramework) Finalize() error {
7783
return nil
7884
}
7985

80-
// isEnabled checks if client certificate mapper is enabled
81-
// Default is true (enabled) to support mTLS scenarios unless explicitly disabled
82-
func (c *ClientCertificateMapperFramework) isEnabled() bool {
83-
// Check JBP_CONFIG_CLIENT_CERTIFICATE_MAPPER environment variable
86+
func (c *ClientCertificateMapperFramework) loadConfig() (*clientCertificateMapperConfig, error) {
87+
// initialize default values
88+
mapperConfig := clientCertificateMapperConfig{
89+
Enabled: true,
90+
}
8491
config := os.Getenv("JBP_CONFIG_CLIENT_CERTIFICATE_MAPPER")
85-
86-
// Parse the config to check for enabled: false
87-
// For simplicity, if JBP_CONFIG_CLIENT_CERTIFICATE_MAPPER is set and contains "enabled", check its value
88-
// A more robust implementation would parse YAML
8992
if config != "" {
90-
// Simple check: if it contains "enabled: false" or "'enabled': false"
91-
if contains(config, "enabled: false") || contains(config, "'enabled': false") {
92-
return false
93+
yamlHandler := common.YamlHandler{}
94+
err := yamlHandler.ValidateFields([]byte(config), &mapperConfig)
95+
if err != nil {
96+
c.context.Log.Warning("Unknown user config values: %s", err.Error())
9397
}
94-
if contains(config, "enabled: true") || contains(config, "'enabled': true") {
95-
return true
98+
// overlay JBP_CONFIG_CLIENT_CERTIFICATE_MAPPER over default values
99+
if err = yamlHandler.Unmarshal([]byte(config), &mapperConfig); err != nil {
100+
return nil, fmt.Errorf("failed to parse JBP_CONFIG_CLIENT_CERTIFICATE_MAPPER: %w", err)
96101
}
97102
}
103+
return &mapperConfig, nil
104+
}
98105

99-
// Default to enabled (to support mTLS client certificate authentication)
100-
return true
106+
type clientCertificateMapperConfig struct {
107+
Enabled bool `yaml:"enabled"`
108+
}
109+
110+
// isEnabled checks if client certificate mapper is enabled
111+
func (c *clientCertificateMapperConfig) isEnabled() bool {
112+
return c.Enabled
101113
}

src/java/frameworks/container_security_provider.go

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,17 @@ func (c *ContainerSecurityProviderFramework) Finalize() error {
9595
return fmt.Errorf("failed to write security properties: %w", err)
9696
}
9797

98+
config, err := c.loadConfig()
99+
if err != nil {
100+
c.context.Log.Warning("Failed to load container security provider config: %s", err.Error())
101+
}
98102
// Add key manager and trust manager configuration if specified
99-
keyManagerEnabled := c.getKeyManagerEnabled()
103+
keyManagerEnabled := config.getKeyManagerEnabled()
100104
if keyManagerEnabled != "" {
101105
javaOpts += fmt.Sprintf(" -Dorg.cloudfoundry.security.keymanager.enabled=%s", keyManagerEnabled)
102106
}
103107

104-
trustManagerEnabled := c.getTrustManagerEnabled()
108+
trustManagerEnabled := config.getTrustManagerEnabled()
105109
if trustManagerEnabled != "" {
106110
javaOpts += fmt.Sprintf(" -Dorg.cloudfoundry.security.trustmanager.enabled=%s", trustManagerEnabled)
107111
}
@@ -214,44 +218,38 @@ func (c *ContainerSecurityProviderFramework) getDefaultSecurityProviders() []str
214218
}
215219
}
216220

217-
// getKeyManagerEnabled returns the key_manager_enabled configuration value
218-
func (c *ContainerSecurityProviderFramework) getKeyManagerEnabled() string {
219-
config := os.Getenv("JBP_CONFIG_CONTAINER_SECURITY_PROVIDER")
220-
if config == "" {
221-
return ""
221+
func (c *ContainerSecurityProviderFramework) loadConfig() (*containerSecurityProviderConfig, error) {
222+
// initialize default values
223+
secConfig := containerSecurityProviderConfig{
224+
KeyManagerEnabled: "",
225+
TrustManagerEnabled: "",
222226
}
223-
224-
// Parse configuration for key_manager_enabled
225-
// Format: {key_manager_enabled: true} or {'key_manager_enabled': 'true'}
226-
if contains(config, "key_manager_enabled") {
227-
if contains(config, "true") {
228-
return "true"
227+
config := os.Getenv("JBP_CONFIG_CONTAINER_SECURITY_PROVIDER")
228+
if config != "" {
229+
yamlHandler := common.YamlHandler{}
230+
err := yamlHandler.ValidateFields([]byte(config), &secConfig)
231+
if err != nil {
232+
c.context.Log.Warning("Unknown user config values: %s", err.Error())
229233
}
230-
if contains(config, "false") {
231-
return "false"
234+
// overlay JBP_CONFIG_CONTAINER_SECURITY_PROVIDER over default values
235+
if err = yamlHandler.Unmarshal([]byte(config), &secConfig); err != nil {
236+
return nil, fmt.Errorf("failed to parse JBP_CONFIG_CONTAINER_SECURITY_PROVIDER: %w", err)
232237
}
233238
}
239+
return &secConfig, nil
240+
}
234241

235-
return ""
242+
// getKeyManagerEnabled returns the key_manager_enabled configuration value
243+
func (c *containerSecurityProviderConfig) getKeyManagerEnabled() string {
244+
return c.KeyManagerEnabled
236245
}
237246

238247
// getTrustManagerEnabled returns the trust_manager_enabled configuration value
239-
func (c *ContainerSecurityProviderFramework) getTrustManagerEnabled() string {
240-
config := os.Getenv("JBP_CONFIG_CONTAINER_SECURITY_PROVIDER")
241-
if config == "" {
242-
return ""
243-
}
244-
245-
// Parse configuration for trust_manager_enabled
246-
// Format: {trust_manager_enabled: true} or {'trust_manager_enabled': 'true'}
247-
if contains(config, "trust_manager_enabled") {
248-
if contains(config, "true") {
249-
return "true"
250-
}
251-
if contains(config, "false") {
252-
return "false"
253-
}
254-
}
248+
func (c *containerSecurityProviderConfig) getTrustManagerEnabled() string {
249+
return c.TrustManagerEnabled
250+
}
255251

256-
return ""
252+
type containerSecurityProviderConfig struct {
253+
KeyManagerEnabled string `yaml:"key_manager_enabled"`
254+
TrustManagerEnabled string `yaml:"trust_manager_enabled"`
257255
}

0 commit comments

Comments
 (0)