Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.iceberg.rest.credentials;

import java.util.Map;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.immutables.value.Value;

@Value.Immutable
public interface Credential {
String prefix();

Map<String, String> config();

@Value.Check
default void validate() {
Preconditions.checkArgument(!prefix().isEmpty(), "Invalid prefix: must be non-empty");
Preconditions.checkArgument(!config().isEmpty(), "Invalid config: must be non-empty");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.iceberg.rest.credentials;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
import java.util.Map;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.util.JsonUtil;

public class CredentialParser {
private static final String PREFIX = "prefix";
private static final String CONFIG = "config";

private CredentialParser() {}

public static String toJson(Credential credential) {
return toJson(credential, false);
}

public static String toJson(Credential credential, boolean pretty) {
return JsonUtil.generate(gen -> toJson(credential, gen), pretty);
}

public static void toJson(Credential credential, JsonGenerator gen) throws IOException {
Preconditions.checkArgument(null != credential, "Invalid credential: null");

gen.writeStartObject();

gen.writeStringField(PREFIX, credential.prefix());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as below I think we need validation that the prefix isn't empty before serializing it?

JsonUtil.writeStringMap(CONFIG, credential.config(), gen);

gen.writeEndObject();
}

public static Credential fromJson(String json) {
return JsonUtil.parse(json, CredentialParser::fromJson);
}

public static Credential fromJson(JsonNode json) {
Preconditions.checkArgument(null != json, "Cannot parse credential from null object");
String prefix = JsonUtil.getString(PREFIX, json);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need a check to make sure prefix isn't empty?

Copy link
Contributor Author

@nastra nastra Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm yeah good point, we should maybe do the same for the config to make sure the config isn't empty. I've done that and added a test

Map<String, String> config = JsonUtil.getStringMap(CONFIG, json);
return ImmutableCredential.builder().prefix(prefix).config(config).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@
*/
package org.apache.iceberg.rest.responses;

import java.util.List;
import java.util.Map;
import org.apache.iceberg.TableMetadata;
import org.apache.iceberg.relocated.com.google.common.base.MoreObjects;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.iceberg.rest.RESTResponse;
import org.apache.iceberg.rest.credentials.Credential;

/**
* A REST response that is used when a table is successfully loaded.
Expand All @@ -40,16 +44,21 @@ public class LoadTableResponse implements RESTResponse {
private TableMetadata metadata;
private Map<String, String> config;
private TableMetadata metadataWithLocation;
private List<Credential> credentials;

public LoadTableResponse() {
// Required for Jackson deserialization
}

private LoadTableResponse(
String metadataLocation, TableMetadata metadata, Map<String, String> config) {
String metadataLocation,
TableMetadata metadata,
Map<String, String> config,
List<Credential> credentials) {
this.metadataLocation = metadataLocation;
this.metadata = metadata;
this.config = config;
this.credentials = credentials;
}

@Override
Expand All @@ -74,6 +83,10 @@ public Map<String, String> config() {
return config != null ? config : ImmutableMap.of();
}

public List<Credential> credentials() {
return credentials != null ? credentials : ImmutableList.of();
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
Expand All @@ -91,6 +104,7 @@ public static class Builder {
private String metadataLocation;
private TableMetadata metadata;
private final Map<String, String> config = Maps.newHashMap();
private final List<Credential> credentials = Lists.newArrayList();

private Builder() {}

Expand All @@ -110,9 +124,19 @@ public Builder addAllConfig(Map<String, String> properties) {
return this;
}

public Builder addCredential(Credential credential) {
credentials.add(credential);
return this;
}

public Builder addAllCredentials(List<Credential> credentialsToAdd) {
credentials.addAll(credentialsToAdd);
return this;
}

public LoadTableResponse build() {
Preconditions.checkNotNull(metadata, "Invalid metadata: null");
return new LoadTableResponse(metadataLocation, metadata, config);
return new LoadTableResponse(metadataLocation, metadata, config, credentials);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
import org.apache.iceberg.TableMetadata;
import org.apache.iceberg.TableMetadataParser;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.rest.credentials.Credential;
import org.apache.iceberg.rest.credentials.CredentialParser;
import org.apache.iceberg.util.JsonUtil;

public class LoadTableResponseParser {

private static final String METADATA_LOCATION = "metadata-location";
private static final String METADATA = "metadata";
private static final String CONFIG = "config";
private static final String STORAGE_CREDENTIALS = "storage-credentials";

private LoadTableResponseParser() {}

Expand Down Expand Up @@ -58,6 +61,15 @@ public static void toJson(LoadTableResponse response, JsonGenerator gen) throws
JsonUtil.writeStringMap(CONFIG, response.config(), gen);
}

if (!response.credentials().isEmpty()) {
gen.writeArrayFieldStart(STORAGE_CREDENTIALS);
for (Credential credential : response.credentials()) {
CredentialParser.toJson(credential, gen);
}

gen.writeEndArray();
}

gen.writeEndObject();
}

Expand Down Expand Up @@ -85,6 +97,16 @@ public static LoadTableResponse fromJson(JsonNode json) {
builder.addAllConfig(JsonUtil.getStringMap(CONFIG, json));
}

if (json.hasNonNull(STORAGE_CREDENTIALS)) {
JsonNode credentials = JsonUtil.get(STORAGE_CREDENTIALS, json);
Preconditions.checkArgument(
credentials.isArray(), "Cannot parse credentials from non-array: %s", credentials);

for (JsonNode credential : credentials) {
builder.addCredential(CredentialParser.fromJson(credential));
}
}

return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
*/
package org.apache.iceberg.rest.responses;

import java.util.List;
import java.util.Map;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
import org.apache.iceberg.rest.RESTResponse;
import org.apache.iceberg.rest.credentials.Credential;
import org.apache.iceberg.view.ViewMetadata;
import org.immutables.value.Value;

Expand All @@ -31,6 +34,11 @@ public interface LoadViewResponse extends RESTResponse {

Map<String, String> config();

@Value.Default
default List<Credential> credentials() {
return ImmutableList.of();
}

@Override
default void validate() {
// nothing to validate as it's not possible to create an invalid instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.rest.credentials.Credential;
import org.apache.iceberg.rest.credentials.CredentialParser;
import org.apache.iceberg.util.JsonUtil;
import org.apache.iceberg.view.ViewMetadata;
import org.apache.iceberg.view.ViewMetadataParser;
Expand All @@ -31,6 +33,7 @@ public class LoadViewResponseParser {
private static final String METADATA_LOCATION = "metadata-location";
private static final String METADATA = "metadata";
private static final String CONFIG = "config";
private static final String STORAGE_CREDENTIALS = "storage-credentials";

private LoadViewResponseParser() {}

Expand All @@ -56,6 +59,15 @@ public static void toJson(LoadViewResponse response, JsonGenerator gen) throws I
JsonUtil.writeStringMap(CONFIG, response.config(), gen);
}

if (!response.credentials().isEmpty()) {
gen.writeArrayFieldStart(STORAGE_CREDENTIALS);
for (Credential credential : response.credentials()) {
CredentialParser.toJson(credential, gen);
}

gen.writeEndArray();
}

gen.writeEndObject();
}

Expand All @@ -80,6 +92,16 @@ public static LoadViewResponse fromJson(JsonNode json) {
builder.config(JsonUtil.getStringMap(CONFIG, json));
}

if (json.hasNonNull(STORAGE_CREDENTIALS)) {
JsonNode credentials = JsonUtil.get(STORAGE_CREDENTIALS, json);
Preconditions.checkArgument(
credentials.isArray(), "Cannot parse credentials from non-array: %s", credentials);

for (JsonNode credential : credentials) {
builder.addCredentials(CredentialParser.fromJson(credential));
}
}

return builder.build();
}
}
Loading