|
| 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