Skip to content

Commit 41b0ed6

Browse files
go fmt and added note
1 parent 6574fd3 commit 41b0ed6

File tree

3 files changed

+37
-24
lines changed

3 files changed

+37
-24
lines changed

file_uploader.go

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import (
66
"fmt"
77
"io"
88
"io/fs"
9-
"os"
109
"net/http"
10+
"os"
1111
"strconv"
1212
"strings"
13+
1314
"github.com/aws/aws-sdk-go/aws"
1415
"github.com/aws/aws-sdk-go/aws/credentials"
1516
"github.com/aws/aws-sdk-go/aws/session"
@@ -21,14 +22,22 @@ type FileUploader interface {
2122
UploadFile(src, dst string, meta *MetaData) error
2223
}
2324

25+
// NOTE(sean) Not a big deal but we can probably just use the FileUploader as the primary abstraction
26+
// rather than introducing UploaderConfig. The details of the config can be tracked as part of the
27+
// specific FileUploader internals.
28+
//
29+
// As an example, a FileUploader which simply logs that UploadFile would have been called and does
30+
// nothing else probably doesn't really need to know about endpoints or buckets.
31+
//
32+
// I'll leave this as a simple item to clean up when we have some free cycles.
2433
type UploaderConfig interface {
2534
GetEndpoint() string
2635
GetBucket() string
2736
}
2837

2938
type PelicanFileUploaderConfig struct {
30-
Endpoint string
31-
Bucket string
39+
Endpoint string
40+
Bucket string
3241
}
3342

3443
type S3FileUploaderConfig struct {
@@ -45,9 +54,9 @@ type s3FileUploader struct {
4554
}
4655

4756
type pelicanFileUploader struct {
48-
config PelicanFileUploaderConfig
49-
client http.Client
50-
jm JwtManager
57+
config PelicanFileUploaderConfig
58+
client http.Client
59+
jm JwtManager
5160
}
5261

5362
// GetEndpoint returns the endpoint for S3.
@@ -90,12 +99,12 @@ func NewS3FileUploader(config S3FileUploaderConfig) (*s3FileUploader, error) {
9099
}, nil
91100
}
92101

93-
//Initialize a new file uploader for Pelican by passing in a config, an initliaze JwtManager, and public key's id
102+
// Initialize a new file uploader for Pelican by passing in a config, an initliaze JwtManager, and public key's id
94103
func NewPelicanFileUploader(config PelicanFileUploaderConfig, jm JwtManager) (*pelicanFileUploader, error) {
95104
return &pelicanFileUploader{
96-
config: config,
97-
client: http.Client{},
98-
jm: jm,
105+
config: config,
106+
client: http.Client{},
107+
jm: jm,
99108
}, nil
100109
}
101110

@@ -153,14 +162,18 @@ func (up *pelicanFileUploader) UploadFile(src, dst string, meta *MetaData) error
153162

154163
//Upload the file to Pelican
155164
req, err := http.NewRequest("PUT", fmt.Sprintf("%s/%s/%s", up.config.Endpoint, up.config.Bucket, dst), f)
156-
if err != nil {return err}
165+
if err != nil {
166+
return err
167+
}
157168
req.Header.Set("Authorization", "Bearer "+string(up.jm.SignedJwtToken))
158169
resp, err := up.client.Do(req)
159-
if err != nil {return err}
170+
if err != nil {
171+
return err
172+
}
160173
defer resp.Body.Close()
161174

162175
// Check response status
163-
if resp.StatusCode == http.StatusForbidden {
176+
if resp.StatusCode == http.StatusForbidden {
164177
// JWT token expired, regenerate it
165178
token, err := up.jm.generateJwtToken(&up.jm.PublicKeyID)
166179
if err != nil {
@@ -169,7 +182,7 @@ func (up *pelicanFileUploader) UploadFile(src, dst string, meta *MetaData) error
169182
up.jm.SignedJwtToken = token
170183

171184
// retry uploading file
172-
err = up.UploadFile(src,dst,meta)
185+
err = up.UploadFile(src, dst, meta)
173186
if err != nil {
174187
return err
175188
}
@@ -180,7 +193,7 @@ func (up *pelicanFileUploader) UploadFile(src, dst string, meta *MetaData) error
180193
return fmt.Errorf("error reading response body: %v", err)
181194
}
182195
return fmt.Errorf("pelican uploader failed, non-OK HTTP status: %v \n response body: %s", resp.Status, body)
183-
}
196+
}
184197

185198
uploadFileMetrics(stat, meta)
186199

main.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,14 @@ func mustGetS3UploaderConfig() S3FileUploaderConfig {
236236

237237
func mustGetPelicanUploaderConfig() PelicanFileUploaderConfig {
238238
return PelicanFileUploaderConfig{
239-
Endpoint: mustGetEnv("LOADER_PELICAN_ENDPOINT"),
240-
Bucket: mustGetEnv("LOADER_PELICAN_BUCKET"),
239+
Endpoint: mustGetEnv("LOADER_PELICAN_ENDPOINT"),
240+
Bucket: mustGetEnv("LOADER_PELICAN_BUCKET"),
241241
}
242242
}
243243

244-
//This function retrieves the env variables for configuring Jwt Manager and makes sure they exist.
245-
func mustGetJwtManagerConfig() (PublicKeyConfigURL string, IssuerKeyPath string,keyID string) {
246-
return mustGetEnv("JWT_PUBLIC_KEY_CONFIG_URL"),mustGetEnv("JWT_ISSUER_KEY_PATH"),mustGetEnv("JWT_PUBLIC_KEY_ID")
244+
// This function retrieves the env variables for configuring Jwt Manager and makes sure they exist.
245+
func mustGetJwtManagerConfig() (PublicKeyConfigURL string, IssuerKeyPath string, keyID string) {
246+
return mustGetEnv("JWT_PUBLIC_KEY_CONFIG_URL"), mustGetEnv("JWT_ISSUER_KEY_PATH"), mustGetEnv("JWT_PUBLIC_KEY_ID")
247247
}
248248

249249
func main() {
@@ -266,7 +266,7 @@ func main() {
266266
DataDir: getEnv("LOADER_DATA_DIR", "/data"),
267267
Config: mustGetPelicanUploaderConfig(),
268268
}
269-
log.Printf("using Pelican at %s in bucket %s",config.Config.GetEndpoint(), config.Config.GetBucket())
269+
log.Printf("using Pelican at %s in bucket %s", config.Config.GetEndpoint(), config.Config.GetBucket())
270270
default:
271271
// Handle unknown or unsupported type
272272
log.Fatalf("unsupported storage type: %s", stor_type)

token.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6+
"github.com/golang-jwt/jwt/v5"
67
"net/http"
78
"os"
89
"time"
9-
"github.com/golang-jwt/jwt/v5"
1010
)
1111

1212
// JwtManager manages operations related to JWT tokens
1313
type JwtManager struct {
1414
publicKeyConfigURL string
1515
issuerKeyPath string
16-
PublicKeyID string
16+
PublicKeyID string
1717
SignedJwtToken string
1818
publicKeyConfig *PublicKeyConfig
1919
jwks *Jwks
@@ -140,7 +140,7 @@ func (jm *JwtManager) _signJwtToken(token *jwt.Token) (string, error) {
140140
return "", fmt.Errorf("error signing token: %v", err)
141141
}
142142

143-
fmt.Println("Generated JWT token using ",jm.issuerKeyPath)
143+
fmt.Println("Generated JWT token using ", jm.issuerKeyPath)
144144

145145
return signedToken, nil
146146
}

0 commit comments

Comments
 (0)