diff --git a/connector/aws/s3/src/main/java/com/blazebit/query/connector/aws/s3/AwsPublicAccessBlockConfiguration.java b/connector/aws/s3/src/main/java/com/blazebit/query/connector/aws/s3/AwsPublicAccessBlockConfiguration.java new file mode 100644 index 00000000..0df59950 --- /dev/null +++ b/connector/aws/s3/src/main/java/com/blazebit/query/connector/aws/s3/AwsPublicAccessBlockConfiguration.java @@ -0,0 +1,23 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * Copyright Blazebit + */ +package com.blazebit.query.connector.aws.s3; + +import com.blazebit.query.connector.aws.base.AwsWrapper; +import software.amazon.awssdk.services.s3.model.PublicAccessBlockConfiguration; + +/** + * @author Donghwi Kim + * @since 1.0.0 + */ +public class AwsPublicAccessBlockConfiguration extends AwsWrapper { + public AwsPublicAccessBlockConfiguration(String accountId, String region, String resourceId, PublicAccessBlockConfiguration payload) { + super( accountId, region, resourceId, payload ); + } + + @Override + public PublicAccessBlockConfiguration getPayload() { + return super.getPayload(); + } +} diff --git a/connector/aws/s3/src/main/java/com/blazebit/query/connector/aws/s3/AwsS3SchemaProvider.java b/connector/aws/s3/src/main/java/com/blazebit/query/connector/aws/s3/AwsS3SchemaProvider.java index 4b2af74e..49f8a89b 100644 --- a/connector/aws/s3/src/main/java/com/blazebit/query/connector/aws/s3/AwsS3SchemaProvider.java +++ b/connector/aws/s3/src/main/java/com/blazebit/query/connector/aws/s3/AwsS3SchemaProvider.java @@ -20,7 +20,8 @@ public final class AwsS3SchemaProvider implements QuerySchemaProvider { @Override public Set> resolveSchemaObjects(ConfigurationProvider configurationProvider) { return Set.of( - BucketDataFetcher.INSTANCE + BucketDataFetcher.INSTANCE, + PublicAccessBlockConfigurationFetcher.INSTANCE ); } } diff --git a/connector/aws/s3/src/main/java/com/blazebit/query/connector/aws/s3/BucketDataFetcher.java b/connector/aws/s3/src/main/java/com/blazebit/query/connector/aws/s3/BucketDataFetcher.java index d7d1515b..9e2e332e 100644 --- a/connector/aws/s3/src/main/java/com/blazebit/query/connector/aws/s3/BucketDataFetcher.java +++ b/connector/aws/s3/src/main/java/com/blazebit/query/connector/aws/s3/BucketDataFetcher.java @@ -40,13 +40,13 @@ public List fetch(DataFetchContext context) { List list = new ArrayList<>(); for ( AwsConnectorConfig.Account account : accounts ) { for ( Region region : account.getRegions() ) { - S3ClientBuilder ec2ClientBuilder = S3Client.builder() + S3ClientBuilder s3ClientBuilder = S3Client.builder() .region( region ) .credentialsProvider( account.getCredentialsProvider() ); if ( sdkHttpClient != null ) { - ec2ClientBuilder.httpClient( sdkHttpClient ); + s3ClientBuilder.httpClient( sdkHttpClient ); } - try (S3Client client = ec2ClientBuilder.build()) { + try (S3Client client = s3ClientBuilder.build()) { for ( Bucket bucket : client.listBuckets().buckets() ) { list.add( new AwsBucket( account.getAccountId(), @@ -61,7 +61,7 @@ public List fetch(DataFetchContext context) { return list; } catch (RuntimeException e) { - throw new DataFetcherException( "Could not fetch bucket list", e ); + throw new DataFetcherException( "Could not fetch public access block configuration list", e ); } } diff --git a/connector/aws/s3/src/main/java/com/blazebit/query/connector/aws/s3/PublicAccessBlockConfigurationFetcher.java b/connector/aws/s3/src/main/java/com/blazebit/query/connector/aws/s3/PublicAccessBlockConfigurationFetcher.java new file mode 100644 index 00000000..fa52b419 --- /dev/null +++ b/connector/aws/s3/src/main/java/com/blazebit/query/connector/aws/s3/PublicAccessBlockConfigurationFetcher.java @@ -0,0 +1,74 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * Copyright Blazebit + */ +package com.blazebit.query.connector.aws.s3; + +import com.blazebit.query.connector.aws.base.AwsConnectorConfig; +import com.blazebit.query.connector.aws.base.AwsConventionContext; +import com.blazebit.query.connector.base.DataFormats; +import com.blazebit.query.spi.DataFetchContext; +import com.blazebit.query.spi.DataFetcher; +import com.blazebit.query.spi.DataFetcherException; +import com.blazebit.query.spi.DataFormat; +import software.amazon.awssdk.http.SdkHttpClient; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.s3.S3Client; +import software.amazon.awssdk.services.s3.S3ClientBuilder; +import software.amazon.awssdk.services.s3.model.Bucket; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +/** + * @author Donghwi Kim + * @since 1.0.0 + */ +public class PublicAccessBlockConfigurationFetcher implements DataFetcher, Serializable { + + public static final PublicAccessBlockConfigurationFetcher INSTANCE = new PublicAccessBlockConfigurationFetcher(); + + private PublicAccessBlockConfigurationFetcher() { + } + + @Override + public List fetch(DataFetchContext context) { + try { + List accounts = AwsConnectorConfig.ACCOUNT.getAll( context ); + SdkHttpClient sdkHttpClient = AwsConnectorConfig.HTTP_CLIENT.find( context ); + List list = new ArrayList<>(); + for ( AwsConnectorConfig.Account account : accounts ) { + for ( Region region : account.getRegions() ) { + S3ClientBuilder s3ClientBuilder = S3Client.builder() + .region( region ) + .credentialsProvider( account.getCredentialsProvider() ); + if ( sdkHttpClient != null ) { + s3ClientBuilder.httpClient( sdkHttpClient ); + } + try (S3Client client = s3ClientBuilder.build()) { + for ( Bucket bucket : client.listBuckets().buckets() ) { + var publicAccessBlockResponse = client.getPublicAccessBlock(r->r.bucket( bucket.name() )); + var publicAccessBlockConfiguration = publicAccessBlockResponse.publicAccessBlockConfiguration(); + list.add( new AwsPublicAccessBlockConfiguration( + account.getAccountId(), + region.id(), + bucket.name(), + publicAccessBlockConfiguration + ) ); + } + } + } + } + return list; + } + catch (RuntimeException e) { + throw new DataFetcherException( "Could not fetch public access block configuration list", e ); + } + } + + @Override + public DataFormat getDataFormat() { + return DataFormats.componentMethodConvention( AwsPublicAccessBlockConfiguration.class, AwsConventionContext.INSTANCE ); + } +} diff --git a/examples/app/src/main/java/com/blazebit/query/app/Main.java b/examples/app/src/main/java/com/blazebit/query/app/Main.java index a514e84c..8dc0debf 100644 --- a/examples/app/src/main/java/com/blazebit/query/app/Main.java +++ b/examples/app/src/main/java/com/blazebit/query/app/Main.java @@ -37,6 +37,7 @@ import com.blazebit.query.connector.aws.rds.AwsDBInstance; import com.blazebit.query.connector.aws.route53.AwsHealthCheck; import com.blazebit.query.connector.aws.route53.AwsHostedZone; +import com.blazebit.query.connector.aws.s3.AwsPublicAccessBlockConfiguration; import com.blazebit.query.connector.aws.s3.AwsBucket; import com.blazebit.query.connector.azure.graph.AzureGraphAlert; import com.blazebit.query.connector.azure.graph.AzureGraphApplication; @@ -261,6 +262,8 @@ public static void main(String[] args) throws Exception { queryContextBuilder.registerSchemaObjectAlias( AwsHealthCheck.class, "AwsHealthCheck" ); // S3 queryContextBuilder.registerSchemaObjectAlias( AwsBucket.class, "AwsBucket" ); + queryContextBuilder.registerSchemaObjectAlias( AwsPublicAccessBlockConfiguration.class, "AwsPublicAccessBlockConfiguration" ); + // Gitlab queryContextBuilder.registerSchemaObjectAlias( Project.class, "GitlabProject" ); @@ -482,6 +485,12 @@ private static void testAws(QuerySession session) { List awsBucketResult = awsBucketQuery.getResultList(); System.out.println("AwsBuckets"); print(awsBucketResult); + + TypedQuery awsPublicAccessBlockConfigurationQuery = session.createQuery( + "select f.* from AwsPublicAccessBlockConfiguration f" ); + List awsPublicAccessBlockConfigurationResult = awsPublicAccessBlockConfigurationQuery.getResultList(); + System.out.println("AwsPublicAccessBlockConfiguration"); + print(awsPublicAccessBlockConfigurationResult); } private static void testGitlab(QuerySession session) {