Skip to content

Commit 46b8b15

Browse files
committed
auth into separate class
1 parent eb21a84 commit 46b8b15

File tree

12 files changed

+340
-194
lines changed

12 files changed

+340
-194
lines changed

server/app/src/main/java/io/whitefox/api/deltasharing/server/DeltaSharesApiImpl.java

+51-31
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,12 @@ public DeltaSharesApiImpl(
4848
@Override
4949
public Response getShare(String share) {
5050
return wrapExceptions(
51-
() ->
52-
optionalToNotFound(shareService.getShare(share),
53-
foundShare ->
54-
shareToForbidden(foundShare, s -> {
55-
var resultShare = new Share().name(s.name()).id(s.id());
56-
return Response.ok(resultShare).build();
57-
})),
51+
() -> optionalToNotFound(
52+
shareService.getShare(share),
53+
foundShare -> shareToForbidden(foundShare, s -> {
54+
var resultShare = new Share().name(s.name()).id(s.id());
55+
return Response.ok(resultShare).build();
56+
})),
5857
exceptionToResponse);
5958
}
6059

@@ -87,9 +86,15 @@ public Response getTableMetadata(
8786
clientCapabilitiesMapper.parseDeltaSharingCapabilities(deltaSharingCapabilities);
8887
return optionalToNotFound(
8988
deltaSharesService.getTableMetadata(
90-
share, schema, table, startingTimestamp, clientCapabilities),
89+
share,
90+
schema,
91+
table,
92+
startingTimestamp,
93+
clientCapabilities,
94+
getRequestPrincipal()),
9195
m -> optionalToNotFound(
92-
deltaSharesService.getTableVersion(share, schema, table, startingTimestamp),
96+
deltaSharesService.getTableVersion(
97+
share, schema, table, startingTimestamp, getRequestPrincipal()),
9398
v -> Response.ok(
9499
tableResponseSerializer.serialize(
95100
DeltaMappers.toTableResponseMetadata(m)),
@@ -106,33 +111,39 @@ public Response getTableMetadata(
106111
@Override
107112
public Response getTableVersion(
108113
String share, String schema, String table, String startingTimestampStr) {
109-
110114
return wrapExceptions(
111-
() ->
112-
optionalToNotFound(shareService.getShare(share), foundShare -> shareToForbidden(foundShare, s -> {
115+
() -> {
113116
var startingTimestamp = parseTimestamp(startingTimestampStr);
114117
return optionalToNotFound(
115-
deltaSharesService.getTableVersion(share, schema, table, startingTimestamp),
118+
deltaSharesService.getTableVersion(
119+
share, schema, table, startingTimestamp, getRequestPrincipal()),
116120
t -> Response.ok().header(DELTA_TABLE_VERSION_HEADER, t).build());
117-
})),
121+
},
118122
exceptionToResponse);
119123
}
120124

121125
@Override
122126
public Response listALLTables(String share, Integer maxResults, String pageToken) {
123127
return wrapExceptions(
124-
() ->
125-
optionalToNotFound(shareService.getShare(share), foundShare -> shareToForbidden(foundShare, s ->
126-
optionalToNotFound(
127-
deltaSharesService.listTablesOfShare(
128-
share, parseToken(pageToken), Optional.ofNullable(maxResults)),
129-
c -> Response.ok(c.getToken()
130-
.map(t -> new ListTablesResponse()
131-
.items(mapList(c.getContent(), DeltaMappers::table2api))
132-
.nextPageToken(tokenEncoder.encodePageToken(t)))
133-
.orElse(new ListTablesResponse()
134-
.items(mapList(c.getContent(), DeltaMappers::table2api))))
135-
.build()))),
128+
() -> optionalToNotFound(
129+
shareService.getShare(share),
130+
foundShare -> shareToForbidden(
131+
foundShare,
132+
s -> optionalToNotFound(
133+
deltaSharesService.listTablesOfShare(
134+
share,
135+
parseToken(pageToken),
136+
Optional.ofNullable(maxResults),
137+
getRequestPrincipal()),
138+
c -> Response.ok(c.getToken()
139+
.map(
140+
t -> new ListTablesResponse()
141+
.items(mapList(c.getContent(), DeltaMappers::table2api))
142+
.nextPageToken(tokenEncoder.encodePageToken(t)))
143+
.orElse(
144+
new ListTablesResponse()
145+
.items(mapList(c.getContent(), DeltaMappers::table2api))))
146+
.build()))),
136147
exceptionToResponse);
137148
}
138149

@@ -141,7 +152,11 @@ public Response listSchemas(String share, Integer maxResults, String pageToken)
141152
return wrapExceptions(
142153
() -> optionalToNotFound(
143154
deltaSharesService
144-
.listSchemas(share, parseToken(pageToken), Optional.ofNullable(maxResults))
155+
.listSchemas(
156+
share,
157+
parseToken(pageToken),
158+
Optional.ofNullable(maxResults),
159+
getRequestPrincipal())
145160
.map(ct -> ct.getToken()
146161
.map(t -> new ListSchemasResponse()
147162
.nextPageToken(tokenEncoder.encodePageToken(t))
@@ -156,8 +171,8 @@ public Response listSchemas(String share, Integer maxResults, String pageToken)
156171
public Response listShares(Integer maxResults, String pageToken) {
157172
return wrapExceptions(
158173
() -> {
159-
var c =
160-
deltaSharesService.listShares(parseToken(pageToken), Optional.ofNullable(maxResults));
174+
var c = deltaSharesService.listShares(
175+
parseToken(pageToken), Optional.ofNullable(maxResults), getRequestPrincipal());
161176
var response =
162177
new ListShareResponse().items(mapList(c.getContent(), DeltaMappers::share2api));
163178
return Response.ok(c.getToken()
@@ -173,7 +188,11 @@ public Response listTables(String share, String schema, Integer maxResults, Stri
173188
return wrapExceptions(
174189
() -> optionalToNotFound(
175190
deltaSharesService.listTables(
176-
share, schema, parseToken(pageToken), Optional.ofNullable(maxResults)),
191+
share,
192+
schema,
193+
parseToken(pageToken),
194+
Optional.ofNullable(maxResults),
195+
getRequestPrincipal()),
177196
c -> Response.ok(c.getToken()
178197
.map(t -> new ListTablesResponse()
179198
.items(mapList(c.getContent(), DeltaMappers::table2api))
@@ -206,7 +225,8 @@ public Response queryTable(
206225
schema,
207226
table,
208227
DeltaMappers.api2ReadTableRequest(queryRequest),
209-
clientCapabilitiesMapper.parseDeltaSharingCapabilities(deltaSharingCapabilities));
228+
clientCapabilitiesMapper.parseDeltaSharingCapabilities(deltaSharingCapabilities),
229+
getRequestPrincipal());
210230
var serializedReadResult =
211231
tableQueryResponseSerializer.serialize(DeltaMappers.readTableResult2api(readResult));
212232
return Response.ok(serializedReadResult, ndjsonMediaType)

server/app/src/main/java/io/whitefox/api/server/ApiUtils.java

+3-16
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.time.OffsetDateTime;
1313
import java.time.format.DateTimeFormatter;
1414
import java.time.format.DateTimeParseException;
15+
import java.util.Map;
1516
import java.util.Optional;
1617
import java.util.function.Function;
1718
import java.util.function.Supplier;
@@ -71,22 +72,8 @@ default <T> Response optionalToNotFound(Optional<T> opt, Function<T, Response> f
7172
}
7273

7374
default Response shareToForbidden(Share value, Function<Share, Response> fn) {
74-
if (value.recipients().contains(getRequestPrincipal()))
75-
return fn.apply(value);
76-
else
77-
return Response.status(Response.Status.FORBIDDEN).build();
78-
}
79-
80-
default String getResponseFormatHeader(Map<String, String> deltaSharingCapabilities) {
81-
return String.format(
82-
"%s=%s",
83-
DeltaHeaders.DELTA_SHARING_RESPONSE_FORMAT, getResponseFormat(deltaSharingCapabilities));
84-
}
85-
86-
default String getResponseFormat(Map<String, String> deltaSharingCapabilities) {
87-
return deltaSharingCapabilities.getOrDefault(
88-
DeltaHeaders.DELTA_SHARING_RESPONSE_FORMAT,
89-
DeltaSharedTable.DeltaShareTableFormat.RESPONSE_FORMAT_PARQUET);
75+
if (value.recipients().contains(getRequestPrincipal())) return fn.apply(value);
76+
else return Response.status(Response.Status.FORBIDDEN).build();
9077
}
9178

9279
default Principal getRequestPrincipal() {

server/app/src/test/java/io/whitefox/api/deltasharing/server/DeltaSharesApiImplAwsTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void updateStorageManagerWithS3Tables() {
9191
"s3share",
9292
s3IcebergTable1(s3TestConfig, awsGlueTestConfig))),
9393
"s3share")),
94-
new Principal("Mr fox"),
94+
new Principal("Mr. Fox"),
9595
0L));
9696
}
9797

server/app/src/test/java/io/whitefox/api/server/ShareV1ApiImplTest.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void createShare() {
5757
.statusCode(201)
5858
.body("name", is("share1"))
5959
.body("comment", is(nullValue()))
60-
.body("recipients", is(hasSize(0)))
60+
.body("recipients", is(hasSize(1)))
6161
.body("schemas", is(hasSize(0)))
6262
.body("createdAt", is(0))
6363
.body("createdBy", is("Mr. Fox"))
@@ -89,7 +89,7 @@ void addRecipientsToShare() {
8989
.statusCode(200)
9090
.body("name", is("share1"))
9191
.body("comment", is(nullValue()))
92-
.body("recipients", is(hasSize(3)))
92+
.body("recipients", is(hasSize(4)))
9393
.body("schemas", is(hasSize(0)))
9494
.body("createdAt", is(0))
9595
.body("createdBy", is("Mr. Fox"))
@@ -105,7 +105,7 @@ void addSameRecipientTwice() {
105105
.statusCode(200)
106106
.body("name", is("share1"))
107107
.body("comment", is(nullValue()))
108-
.body("recipients", is(hasSize(3)))
108+
.body("recipients", is(hasSize(4)))
109109
.body("schemas", is(hasSize(0)))
110110
.body("createdAt", is(0))
111111
.body("createdBy", is("Mr. Fox"))
@@ -121,7 +121,7 @@ void addAnotherRecipient() {
121121
.statusCode(200)
122122
.body("name", is("share1"))
123123
.body("comment", is(nullValue()))
124-
.body("recipients", is(hasSize(4)))
124+
.body("recipients", is(hasSize(5)))
125125
.body("schemas", is(hasSize(0)))
126126
.body("createdAt", is(0))
127127
.body("createdBy", is("Mr. Fox"))
@@ -143,7 +143,7 @@ public void createSchema() {
143143
.statusCode(201)
144144
.body("name", is("share1"))
145145
.body("comment", is(nullValue()))
146-
.body("recipients", is(hasSize(4)))
146+
.body("recipients", is(hasSize(5)))
147147
.body("schemas", is(hasSize(1)))
148148
.body("schemas[0]", is("schema1"))
149149
.body("createdAt", is(0))
@@ -185,7 +185,7 @@ public void addTableToSchema() {
185185
.statusCode(201)
186186
.body("name", is("share1"))
187187
.body("comment", is(nullValue()))
188-
.body("recipients", is(hasSize(4)))
188+
.body("recipients", is(hasSize(5)))
189189
.body("schemas", is(hasSize(1)))
190190
.body("schemas[0]", is("schema1"))
191191
.body("createdAt", is(0))
@@ -200,7 +200,7 @@ ValidatableResponse createEmptyShare(String name) {
200200
.when()
201201
.filter(whitefoxFilter)
202202
.body(
203-
new CreateShareInput().name(name).recipients(List.of()).schemas(List.of()),
203+
new CreateShareInput().name(name).recipients(List.of("Mr. Fox")).schemas(List.of()),
204204
new Jackson2Mapper((cls, charset) -> objectMapper))
205205
.header(new Header("Content-Type", "application/json"))
206206
.post("/whitefox-api/v1/shares")

server/core/build.gradle.kts

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ dependencies {
1919
implementation(enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}"))
2020
// QUARKUS
2121
compileOnly("jakarta.enterprise:jakarta.enterprise.cdi-api")
22-
compileOnly("jakarta.ws.rs:jakarta.ws.rs-api")
22+
implementation("jakarta.ws.rs:jakarta.ws.rs-api")
2323
compileOnly("org.eclipse.microprofile.config:microprofile-config-api")
24-
24+
implementation("org.glassfish.jersey.core:jersey-common:3.1.2")
2525

2626
testFixturesImplementation(String.format("jakarta.inject:jakarta.inject-api:%s", jakartaVersion))
2727
testFixturesImplementation(String.format("org.eclipse.microprofile.config:microprofile-config-api:%s", microprofileConfigVersion))

server/core/src/main/java/io/whitefox/core/Share.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public Share(
5050
id,
5151
schemas,
5252
Optional.empty(),
53-
Set.of(),
53+
Set.of(createPrincipal),
5454
createTime,
5555
createPrincipal,
5656
createTime,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.whitefox.core;
2+
3+
import jakarta.enterprise.context.ApplicationScoped;
4+
import lombok.Data;
5+
6+
public interface WhitefoxAuthorization {
7+
8+
Boolean authorize(Share share, Principal principal);
9+
10+
@Data
11+
@ApplicationScoped
12+
class WhitefoxSimpleAuthorization implements WhitefoxAuthorization {
13+
14+
@Override
15+
public Boolean authorize(Share share, Principal principal) {
16+
return share.recipients().contains(principal);
17+
}
18+
}
19+
}

server/core/src/main/java/io/whitefox/core/services/DeltaSharesService.java

+22-7
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,49 @@
1313
public interface DeltaSharesService {
1414

1515
Optional<Long> getTableVersion(
16-
String share, String schema, String table, Optional<Timestamp> startingTimestamp);
16+
String share,
17+
String schema,
18+
String table,
19+
Optional<Timestamp> startingTimestamp,
20+
Principal principal);
1721

1822
ContentAndToken<List<Share>> listShares(
19-
Optional<ContentAndToken.Token> nextPageToken, Optional<Integer> maxResults);
23+
Optional<ContentAndToken.Token> nextPageToken,
24+
Optional<Integer> maxResults,
25+
Principal currentPrincipal);
2026

2127
Optional<Metadata> getTableMetadata(
2228
String share,
2329
String schema,
2430
String table,
2531
Optional<Timestamp> startingTimestamp,
26-
ClientCapabilities clientCapabilities);
32+
ClientCapabilities clientCapabilities,
33+
Principal currentPrincipal);
2734

2835
Optional<ContentAndToken<List<Schema>>> listSchemas(
29-
String share, Optional<ContentAndToken.Token> nextPageToken, Optional<Integer> maxResults);
36+
String share,
37+
Optional<ContentAndToken.Token> nextPageToken,
38+
Optional<Integer> maxResults,
39+
Principal currentPrincipal);
3040

3141
Optional<ContentAndToken<List<SharedTable>>> listTables(
3242
String share,
3343
String schema,
3444
Optional<ContentAndToken.Token> nextPageToken,
35-
Optional<Integer> maxResults);
45+
Optional<Integer> maxResults,
46+
Principal currentPrincipal);
3647

3748
Optional<ContentAndToken<List<SharedTable>>> listTablesOfShare(
38-
String share, Optional<ContentAndToken.Token> token, Optional<Integer> maxResults);
49+
String share,
50+
Optional<ContentAndToken.Token> token,
51+
Optional<Integer> maxResults,
52+
Principal currentPrincipal);
3953

4054
ReadTableResult queryTable(
4155
String share,
4256
String schema,
4357
String table,
4458
ReadTableRequest queryRequest,
45-
ClientCapabilities clientCapabilities);
59+
ClientCapabilities clientCapabilities,
60+
Principal currentPrincipal);
4661
}

0 commit comments

Comments
 (0)