galovics commented on code in PR #2915: URL: https://github.com/apache/fineract/pull/2915#discussion_r1095501855
########## fineract-provider/src/main/java/org/apache/fineract/infrastructure/s3/AmazonS3ConfigCondition.java: ########## @@ -0,0 +1,51 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://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 org.apache.fineract.infrastructure.s3; + +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.context.annotation.Condition; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.type.AnnotatedTypeMetadata; +import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; +import software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain; + +@Slf4j +public class AmazonS3ConfigCondition implements Condition { + + @Override + public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + Boolean s3ReportEnabled = context.getEnvironment().getRequiredProperty("fineract.report.export.s3.enabled", Boolean.class); + boolean s3BucketIsSet = StringUtils.isNotBlank(context.getEnvironment().getProperty("fineract.report.export.s3.bucket", "")); + log.info("AWS S3 Enabled: {}", s3ReportEnabled); Review Comment: Let's either remove this log message or also log out that the credentials are valid. I think this'll send the wrong message that S3 is enabled even in case the credentials are invalid. ########## fineract-provider/src/main/java/org/apache/fineract/infrastructure/s3/AmazonS3ConfigCondition.java: ########## @@ -0,0 +1,51 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://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 org.apache.fineract.infrastructure.s3; + +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.context.annotation.Condition; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.type.AnnotatedTypeMetadata; +import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; +import software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain; + +@Slf4j +public class AmazonS3ConfigCondition implements Condition { + + @Override + public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + Boolean s3ReportEnabled = context.getEnvironment().getRequiredProperty("fineract.report.export.s3.enabled", Boolean.class); + boolean s3BucketIsSet = StringUtils.isNotBlank(context.getEnvironment().getProperty("fineract.report.export.s3.bucket", "")); + log.info("AWS S3 Enabled: {}", s3ReportEnabled); + log.info("AWS S3 Bucket set: {}", s3ReportEnabled); + return s3ReportEnabled && s3BucketIsSet && isAwsCredentialValid(); + } + + private boolean isAwsCredentialValid() { + try { + DefaultCredentialsProvider.create().resolveCredentials(); + DefaultAwsRegionProviderChain.builder().build().getRegion(); + return true; + } catch (Exception e) { + log.warn("AWS credential is not valid", e); Review Comment: So I think this is not necessarily the best idea either since: 1. the logic is clear, in case its invalid, we'll return false 2. we're saying the credentials are invalid yet in case of incorrectly set region, we also return false and claim that the creds were invalid. ########## fineract-provider/src/main/java/org/apache/fineract/infrastructure/s3/AmazonS3ConfigCondition.java: ########## @@ -0,0 +1,53 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://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 org.apache.fineract.infrastructure.s3; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Condition; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.type.AnnotatedTypeMetadata; +import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; +import software.amazon.awssdk.regions.providers.AwsRegionProvider; + +public class AmazonS3ConfigCondition implements Condition { Review Comment: The point of using PropertiesCondition is that it's gonna be a compilation error if anything changes while in case you rely on the property strings, you don't get any compilation-time checks. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
