Skip to content

Commit

Permalink
Introduce AWS Security Mapping
Browse files Browse the repository at this point in the history
This will allow flexible security mapping for AWS Lake Formation(and AWS Glue) and AWS S3 API calls, allowing for separate IAM roles or IAM credentials for specific users.
  • Loading branch information
imjalpreet committed Jun 4, 2024
1 parent 3492b98 commit 134c888
Show file tree
Hide file tree
Showing 11 changed files with 558 additions and 0 deletions.
10 changes: 10 additions & 0 deletions presto-hive-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
<artifactId>configuration</artifactId>
</dependency>

<dependency>
<groupId>com.facebook.airlift</groupId>
<artifactId>log</artifactId>
</dependency>

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
Expand All @@ -50,6 +55,11 @@
<artifactId>presto-orc</artifactId>
</dependency>

<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-plugin-toolkit</artifactId>
</dependency>

<dependency>
<groupId>com.facebook.presto.hadoop</groupId>
<artifactId>hadoop-apache2</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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
*
* 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 com.facebook.presto.hive.aws.security;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Optional;
import java.util.function.Predicate;
import java.util.regex.Pattern;

import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;

public class AWSSecurityMapping
{
private final Predicate<String> user;
private final Optional<String> iamRole;
private final Optional<BasicAWSCredentials> credentials;

@JsonCreator
public AWSSecurityMapping(
@JsonProperty("user") Optional<Pattern> user,
@JsonProperty("iamRole") Optional<String> iamRole,
@JsonProperty("accessKey") Optional<String> accessKey,
@JsonProperty("secretKey") Optional<String> secretKey)
{
this.user = requireNonNull(user, "user is null")
.map(AWSSecurityMapping::toPredicate)
.orElse(x -> true);

this.iamRole = requireNonNull(iamRole, "iamRole is null");

requireNonNull(accessKey, "accessKey is null");
requireNonNull(secretKey, "secretKey is null");
checkArgument(accessKey.isPresent() == secretKey.isPresent(), "accessKey and secretKey must be provided together");
this.credentials = accessKey.map(access -> new BasicAWSCredentials(access, secretKey.get()));
}

public boolean matches(String user)
{
return this.user.test(user);
}

public Optional<String> getIamRole()
{
return iamRole;
}

public Optional<BasicAWSCredentials> getCredentials()
{
return credentials;
}

@Override
public String toString()
{
return toStringHelper(this)
.add("user", user)
.add("iamRole", iamRole)
.add("credentials", credentials)
.toString();
}

private static Predicate<String> toPredicate(Pattern pattern)
{
return value -> pattern.matcher(value).matches();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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
*
* 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 com.facebook.presto.hive.aws.security;

import com.facebook.airlift.configuration.Config;
import com.facebook.airlift.configuration.ConfigDescription;
import io.airlift.units.Duration;
import io.airlift.units.MinDuration;

import javax.annotation.Nullable;
import javax.validation.constraints.AssertTrue;

import java.io.File;
import java.util.Optional;

import static java.util.Objects.requireNonNull;
import static java.util.concurrent.TimeUnit.SECONDS;

public class AWSSecurityMappingConfig
{
private static final String MAPPING_TYPE = "hive.aws.security-mapping.type";
private static final String CONFIG_FILE = "hive.aws.security-mapping.config-file";
private static final String REFRESH_PERIOD = "hive.aws.security-mapping.refresh-period";
private AWSSecurityMappingType mappingType;
private File configFile;
private Duration refreshPeriod = new Duration(30, SECONDS);

public AWSSecurityMappingType getMappingType()
{
return mappingType;
}

@Config(MAPPING_TYPE)
@ConfigDescription("AWS Security Mapping Type. Possible values: S3 or LAKEFORMATION")
public AWSSecurityMappingConfig setMappingType(AWSSecurityMappingType mappingType)
{
this.mappingType = mappingType;
return this;
}

public Optional<File> getConfigFile()
{
return Optional.ofNullable(configFile);
}

@Nullable
@Config(CONFIG_FILE)
@ConfigDescription("JSON configuration file containing AWS IAM Security mappings")
public AWSSecurityMappingConfig setConfigFile(File configFile)
{
this.configFile = configFile;
return this;
}

public Duration getRefreshPeriod()
{
return refreshPeriod;
}

@MinDuration("0ms")
@Config(REFRESH_PERIOD)
@ConfigDescription("Time interval after which AWS IAM security mapping configuration will be refreshed")
public AWSSecurityMappingConfig setRefreshPeriod(Duration refreshPeriod)
{
this.refreshPeriod = requireNonNull(refreshPeriod, "refreshPeriod is null");
return this;
}

@AssertTrue(message = "MAPPING TYPE(" + MAPPING_TYPE + ") must be configured when AWS Security Mapping Config File(" + CONFIG_FILE + ") is set and vice versa")
public boolean isValidConfiguration()
{
return (getConfigFile().isPresent() && getMappingType() != null) || (!getConfigFile().isPresent() && getMappingType() == null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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
*
* 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 com.facebook.presto.hive.aws.security;

public enum AWSSecurityMappingType
{
S3,
LAKEFORMATION,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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
*
* 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 com.facebook.presto.hive.aws.security;

import com.facebook.presto.spi.security.AccessDeniedException;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableList;

import java.util.List;
import java.util.Optional;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Verify.verify;

public class AWSSecurityMappings
{
private final List<AWSSecurityMapping> awsSecurityMappings;

@JsonCreator
public AWSSecurityMappings(@JsonProperty("mappings") List<AWSSecurityMapping> awsSecurityMappings)
{
checkArgument(awsSecurityMappings != null, "No AWS Security mappings configured");

this.awsSecurityMappings = ImmutableList.copyOf(awsSecurityMappings);
}

public AWSSecurityMapping getAWSLakeFormationSecurityMapping(String user)
{
Optional<AWSSecurityMapping> awsSecurityMapping = awsSecurityMappings.stream()
.filter(mapping -> (mapping.matches(user)))
.findFirst();

if (!awsSecurityMapping.isPresent()) {
throw new AccessDeniedException("No matching AWS Lake Formation Security Mapping");
}

verify(!awsSecurityMapping.get().getCredentials().isPresent(),
"Basic AWS Credentials are not supported for AWS Lake Formation Security Mapping");

verify(awsSecurityMapping.get().getIamRole().isPresent(),
"iamRole is mandatory for AWS Lake Formation Security Mapping");

return awsSecurityMapping.get();
}

public AWSSecurityMapping getAWSS3SecurityMapping(String user)
{
Optional<AWSSecurityMapping> awsSecurityMapping = awsSecurityMappings.stream()
.filter(mapping -> mapping.matches(user))
.findFirst();

return awsSecurityMapping.orElseThrow(() -> new AccessDeniedException("No matching AWS S3 Security Mapping"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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
*
* 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 com.facebook.presto.hive.aws.security;

import com.facebook.airlift.log.Logger;
import com.google.common.base.Suppliers;
import io.airlift.units.Duration;

import java.io.File;
import java.util.Optional;
import java.util.function.Supplier;

import static com.facebook.presto.plugin.base.JsonUtils.parseJson;
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

public class AWSSecurityMappingsSupplier
{
private static final Logger log = Logger.get(AWSSecurityMappingsSupplier.class);
private final Supplier<AWSSecurityMappings> mappingsSupplier;

public AWSSecurityMappingsSupplier(Optional<File> configFile, Duration refreshPeriod)
{
requireNonNull(configFile, "configFile is null");
requireNonNull(refreshPeriod, "refreshPeriod is null");

this.mappingsSupplier = getMappings(configFile, refreshPeriod);
}

private static Supplier<AWSSecurityMappings> getMappings(Optional<File> configFile, Duration refreshPeriod)
{
if (!configFile.isPresent()) {
return null;
}

checkArgument(configFile.get().exists() && configFile.get().isFile(), "AWS Security Mapping config file does not exist: %s", configFile.get());

Supplier<AWSSecurityMappings> supplier = () -> parseJson(configFile.get().toPath(), AWSSecurityMappings.class);

return Suppliers.memoizeWithExpiration(
() -> {
log.debug("Refreshing AWS security mapping configuration from %s", configFile);
return supplier.get();
},
refreshPeriod.toMillis(),
MILLISECONDS);
}

public Supplier<AWSSecurityMappings> getMappingsSupplier()
{
return mappingsSupplier;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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
*
* 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 com.facebook.presto.hive.aws.security;

import static java.util.Objects.requireNonNull;

public class BasicAWSCredentials
{
private final String accessKey;
private final String secretKey;

public BasicAWSCredentials(String accessKey, String secretKey)
{
this.accessKey = requireNonNull(accessKey, "Access key cannot be null");
this.secretKey = requireNonNull(secretKey, "Secret key cannot be null");
}

public String getAWSAccessKeyId()
{
return this.accessKey;
}

public String getAWSSecretKey()
{
return this.secretKey;
}
}
Loading

0 comments on commit 134c888

Please sign in to comment.