Skip to content

Commit

Permalink
Implement ParameterStoreExceptionHappenedAnalyzer for exception that …
Browse files Browse the repository at this point in the history
…happen during loading.
  • Loading branch information
MatejNedic committed Dec 20, 2024
1 parent 425b6c8 commit 7760113
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2013-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.awspring.cloud.autoconfigure.config.parameterstore;

public class AwsParameterPropertySourceNotFoundException extends RuntimeException {

AwsParameterPropertySourceNotFoundException(Exception source) {
super(source);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.springframework.boot.context.config.ConfigData;
import org.springframework.boot.context.config.ConfigDataLoader;
import org.springframework.boot.context.config.ConfigDataLoaderContext;
import org.springframework.boot.context.config.ConfigDataResourceNotFoundException;
import org.springframework.boot.logging.DeferredLogFactory;
import org.springframework.core.env.MapPropertySource;
import org.springframework.lang.Nullable;
Expand All @@ -47,28 +46,23 @@ public ParameterStoreConfigDataLoader(DeferredLogFactory logFactory) {
@Override
@Nullable
public ConfigData load(ConfigDataLoaderContext context, ParameterStoreConfigDataResource resource) {
try {
// resource is disabled if parameter store integration is disabled via
// spring.cloud.aws.parameterstore.enabled=false
if (resource.isEnabled()) {
SsmClient ssm = context.getBootstrapContext().get(SsmClient.class);
ParameterStorePropertySource propertySource = resource.getPropertySources()
.createPropertySource(resource.getContext(), resource.isOptional(), ssm);
if (propertySource != null) {
return new ConfigData(Collections.singletonList(propertySource));
}
else {
return null;
}
// resource is disabled if parameter store integration is disabled via
// spring.cloud.aws.parameterstore.enabled=false
if (resource.isEnabled()) {
SsmClient ssm = context.getBootstrapContext().get(SsmClient.class);
ParameterStorePropertySource propertySource = resource.getPropertySources()
.createPropertySource(resource.getContext(), resource.isOptional(), ssm);
if (propertySource != null) {
return new ConfigData(Collections.singletonList(propertySource));
}
else {
// create dummy empty config data
return new ConfigData(
Collections.singletonList(new MapPropertySource("aws-parameterstore:" + context, Map.of())));
return null;
}
}
catch (Exception e) {
throw new ConfigDataResourceNotFoundException(resource, e);
else {
// create dummy empty config data
return new ConfigData(
Collections.singletonList(new MapPropertySource("aws-parameterstore:" + context, Map.of())));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2013-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.awspring.cloud.autoconfigure.config.parameterstore;

import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
import org.springframework.boot.diagnostics.FailureAnalysis;

public class ParameterStoreExceptionHappenedAnalyzer
extends AbstractFailureAnalyzer<AwsParameterPropertySourceNotFoundException> {

@Override
protected FailureAnalysis analyze(Throwable rootFailure, AwsParameterPropertySourceNotFoundException cause) {
return new FailureAnalysis("Could not import properties from AWS Parameter Store: " + cause.getMessage(),
"Depending on error message determine action course", cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,4 @@ public ParameterStorePropertySource createPropertySource(String context, boolean
return null;
}

static class AwsParameterPropertySourceNotFoundException extends RuntimeException {

AwsParameterPropertySourceNotFoundException(Exception source) {
super(source);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ io.awspring.cloud.autoconfigure.config.s3.S3ConfigDataLoader
# Failure Analyzers
org.springframework.boot.diagnostics.FailureAnalyzer=\
io.awspring.cloud.autoconfigure.config.parameterstore.ParameterStoreMissingKeysFailureAnalyzer, \
io.awspring.cloud.autoconfigure.config.parameterstore.ParameterStoreExceptionHappenedAnalyzer, \
io.awspring.cloud.autoconfigure.config.secretsmanager.SecretsManagerMissingKeysFailureAnalyzer,\
io.awspring.cloud.autoconfigure.config.s3.S3MissingKeysFailureAnalyzer

0 comments on commit 7760113

Please sign in to comment.