Skip to content

Commit 7fbfb76

Browse files
committed
rest: refresh vended credentials
Currently credential lifetimes are tied to the table instead, only load table refreshes the credentials. This commit caches and refreshes the credentials based on the response included in the table load response and refreshes it dynamically.
1 parent de373a5 commit 7fbfb76

File tree

3 files changed

+482
-3
lines changed

3 files changed

+482
-3
lines changed

catalog/rest/rest.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ import (
3838

3939
"github.com/apache/iceberg-go"
4040
"github.com/apache/iceberg-go/catalog"
41-
iceio "github.com/apache/iceberg-go/io"
4241
"github.com/apache/iceberg-go/table"
4342
"github.com/apache/iceberg-go/view"
4443
"github.com/aws/aws-sdk-go-v2/aws"
4544
v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
4645
"github.com/aws/aws-sdk-go-v2/config"
46+
"golang.org/x/sync/semaphore"
4747
)
4848

4949
var _ catalog.Catalog = (*Catalog)(nil)
@@ -630,16 +630,45 @@ func checkValidNamespace(ident table.Identifier) error {
630630
return nil
631631
}
632632

633-
func (r *Catalog) tableFromResponse(ctx context.Context, identifier []string, metadata table.Metadata, loc string, config iceberg.Properties) (*table.Table, error) {
633+
func (r *Catalog) tableFromResponse(_ context.Context, identifier []string, metadata table.Metadata, loc string, config iceberg.Properties) (*table.Table, error) {
634+
refresher := &vendedCredentialRefresher{
635+
mu: semaphore.NewWeighted(1),
636+
identifier: identifier,
637+
location: loc,
638+
props: config,
639+
fetchConfig: r.fetchTableConfig,
640+
}
641+
634642
return table.New(
635643
identifier,
636644
metadata,
637645
loc,
638-
iceio.LoadFSFunc(config, loc),
646+
refresher.loadFS,
639647
r,
640648
), nil
641649
}
642650

651+
func (r *Catalog) fetchTableConfig(ctx context.Context, ident []string) (iceberg.Properties, error) {
652+
ns, tbl, err := splitIdentForPath(ident)
653+
if err != nil {
654+
return nil, err
655+
}
656+
657+
ret, err := doGet[loadTableResponse](ctx, r.baseURI, []string{"namespaces", ns, "tables", tbl},
658+
r.cl, map[int]error{http.StatusNotFound: catalog.ErrNoSuchTable})
659+
if err != nil {
660+
return nil, err
661+
}
662+
663+
config := maps.Clone(r.props)
664+
maps.Copy(config, ret.Metadata.Properties())
665+
for k, v := range ret.Config {
666+
config[k] = v
667+
}
668+
669+
return config, nil
670+
}
671+
643672
func (r *Catalog) ListTables(ctx context.Context, namespace table.Identifier) iter.Seq2[table.Identifier, error] {
644673
return func(yield func(table.Identifier, error) bool) {
645674
pageSize := r.getPageSize(ctx)

catalog/rest/vended_creds.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package rest
19+
20+
import (
21+
"context"
22+
"maps"
23+
"strconv"
24+
"time"
25+
26+
"github.com/apache/iceberg-go"
27+
iceio "github.com/apache/iceberg-go/io"
28+
"golang.org/x/sync/semaphore"
29+
)
30+
31+
const (
32+
keyS3TokenExpiresAtMs = "s3.session-token-expires-at-ms"
33+
keyAdlsSasExpiresAtMs = "adls.sas-token-expires-at-ms"
34+
keyGcsOAuthExpiresAt = "gcs.oauth2.token-expires-at"
35+
keyExpirationTime = "expiration-time"
36+
37+
defaultVendedCredentialsTTL = 60 * time.Minute
38+
)
39+
40+
var credentialExpiryKeys = []string{
41+
keyS3TokenExpiresAtMs,
42+
keyAdlsSasExpiresAtMs,
43+
keyGcsOAuthExpiresAt,
44+
keyExpirationTime,
45+
}
46+
47+
func parseCredentialExpiry(config iceberg.Properties) (time.Time, bool) {
48+
for _, key := range credentialExpiryKeys {
49+
if v, ok := config[key]; ok {
50+
ms, err := strconv.ParseInt(v, 10, 64)
51+
if err == nil && ms > 0 {
52+
return time.UnixMilli(ms), true
53+
}
54+
}
55+
}
56+
57+
return time.Time{}, false
58+
}
59+
60+
type vendedCredentialRefresher struct {
61+
mu *semaphore.Weighted
62+
cachedIO iceio.IO
63+
expiresAt time.Time
64+
65+
identifier []string
66+
location string
67+
props iceberg.Properties
68+
69+
fetchConfig func(ctx context.Context, ident []string) (iceberg.Properties, error)
70+
71+
nowFunc func() time.Time // for testing
72+
}
73+
74+
func (v *vendedCredentialRefresher) now() time.Time {
75+
if v.nowFunc != nil {
76+
return v.nowFunc()
77+
}
78+
79+
return time.Now()
80+
}
81+
82+
func (v *vendedCredentialRefresher) loadFS(ctx context.Context) (iceio.IO, error) {
83+
if err := v.mu.Acquire(ctx, 1); err != nil {
84+
return nil, err
85+
}
86+
defer v.mu.Release(1)
87+
88+
if v.cachedIO != nil && !v.now().After(v.expiresAt) {
89+
return v.cachedIO, nil
90+
}
91+
92+
var config iceberg.Properties
93+
if v.cachedIO == nil {
94+
config = v.props
95+
} else {
96+
freshConfig, err := v.fetchConfig(ctx, v.identifier)
97+
if err != nil {
98+
return v.cachedIO, nil
99+
}
100+
101+
config = make(iceberg.Properties, len(v.props)+len(freshConfig))
102+
maps.Copy(config, v.props)
103+
maps.Copy(config, freshConfig)
104+
}
105+
106+
newIO, err := iceio.LoadFS(ctx, config, v.location)
107+
if err != nil {
108+
if v.cachedIO != nil {
109+
return v.cachedIO, nil
110+
}
111+
112+
return nil, err
113+
}
114+
115+
v.cachedIO = newIO
116+
v.expiresAt = v.expiresAtFromConfig(config)
117+
118+
return v.cachedIO, nil
119+
}
120+
121+
func (v *vendedCredentialRefresher) expiresAtFromConfig(config iceberg.Properties) time.Time {
122+
if exp, ok := parseCredentialExpiry(config); ok {
123+
return exp
124+
}
125+
126+
return v.now().Add(defaultVendedCredentialsTTL)
127+
}

0 commit comments

Comments
 (0)