Skip to content

Commit 89da82b

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 6842055 commit 89da82b

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,14 +38,14 @@ 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"
4746
"golang.org/x/oauth2"
4847
"golang.org/x/oauth2/clientcredentials"
48+
"golang.org/x/sync/semaphore"
4949
)
5050

5151
var _ catalog.Catalog = (*Catalog)(nil)
@@ -675,16 +675,45 @@ func checkValidNamespace(ident table.Identifier) error {
675675
return nil
676676
}
677677

678-
func (r *Catalog) tableFromResponse(ctx context.Context, identifier []string, metadata table.Metadata, loc string, config iceberg.Properties) (*table.Table, error) {
678+
func (r *Catalog) tableFromResponse(_ context.Context, identifier []string, metadata table.Metadata, loc string, config iceberg.Properties) (*table.Table, error) {
679+
refresher := &vendedCredentialRefresher{
680+
mu: semaphore.NewWeighted(1),
681+
identifier: identifier,
682+
location: loc,
683+
props: config,
684+
fetchConfig: r.fetchTableConfig,
685+
}
686+
679687
return table.New(
680688
identifier,
681689
metadata,
682690
loc,
683-
iceio.LoadFSFunc(config, loc),
691+
refresher.loadFS,
684692
r,
685693
), nil
686694
}
687695

696+
func (r *Catalog) fetchTableConfig(ctx context.Context, ident []string) (iceberg.Properties, error) {
697+
ns, tbl, err := splitIdentForPath(ident)
698+
if err != nil {
699+
return nil, err
700+
}
701+
702+
ret, err := doGet[loadTableResponse](ctx, r.baseURI, []string{"namespaces", ns, "tables", tbl},
703+
r.cl, map[int]error{http.StatusNotFound: catalog.ErrNoSuchTable})
704+
if err != nil {
705+
return nil, err
706+
}
707+
708+
config := maps.Clone(r.props)
709+
maps.Copy(config, ret.Metadata.Properties())
710+
for k, v := range ret.Config {
711+
config[k] = v
712+
}
713+
714+
return config, nil
715+
}
716+
688717
func (r *Catalog) ListTables(ctx context.Context, namespace table.Identifier) iter.Seq2[table.Identifier, error] {
689718
return func(yield func(table.Identifier, error) bool) {
690719
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)