|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/golang-jwt/jwt/v4" |
| 10 | + |
| 11 | + ydb "github.com/ydb-platform/ydb-go-sdk/v3" |
| 12 | + "github.com/ydb-platform/ydb-go-sdk/v3/credentials" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + dsn string |
| 17 | + tokenEndpoint string |
| 18 | + keyID string |
| 19 | + privateKeyFile string |
| 20 | + audience string |
| 21 | + issuer string |
| 22 | + subject string |
| 23 | +) |
| 24 | + |
| 25 | +func init() { //nolint:gochecknoinits |
| 26 | + required := []string{"ydb", "private-key-file", "key-id", "token-endpoint"} |
| 27 | + flagSet := flag.NewFlagSet(os.Args[0], flag.ExitOnError) |
| 28 | + flagSet.Usage = func() { |
| 29 | + out := flagSet.Output() |
| 30 | + _, _ = fmt.Fprintf(out, "Usage:\n%s [options]\n", os.Args[0]) |
| 31 | + _, _ = fmt.Fprintf(out, "\nOptions:\n") |
| 32 | + flagSet.PrintDefaults() |
| 33 | + } |
| 34 | + flagSet.StringVar(&dsn, |
| 35 | + "ydb", "", |
| 36 | + "YDB connection string", |
| 37 | + ) |
| 38 | + flagSet.StringVar(&tokenEndpoint, |
| 39 | + "token-endpoint", "", |
| 40 | + "oauth 2.0 token exchange endpoint", |
| 41 | + ) |
| 42 | + flagSet.StringVar(&keyID, |
| 43 | + "key-id", "", |
| 44 | + "key id for jwt token", |
| 45 | + ) |
| 46 | + flagSet.StringVar(&privateKeyFile, |
| 47 | + "private-key-file", "", |
| 48 | + "RSA private key file for jwt token in pem format", |
| 49 | + ) |
| 50 | + flagSet.StringVar(&audience, |
| 51 | + "audience", "", |
| 52 | + "audience", |
| 53 | + ) |
| 54 | + flagSet.StringVar(&issuer, |
| 55 | + "issuer", "", |
| 56 | + "jwt token issuer", |
| 57 | + ) |
| 58 | + flagSet.StringVar(&subject, |
| 59 | + "subject", "", |
| 60 | + "jwt token subject", |
| 61 | + ) |
| 62 | + if err := flagSet.Parse(os.Args[1:]); err != nil { |
| 63 | + flagSet.Usage() |
| 64 | + os.Exit(1) |
| 65 | + } |
| 66 | + flagSet.Visit(func(f *flag.Flag) { |
| 67 | + for i, arg := range required { |
| 68 | + if arg == f.Name { |
| 69 | + required = append(required[:i], required[i+1:]...) |
| 70 | + } |
| 71 | + } |
| 72 | + }) |
| 73 | + if len(required) > 0 { |
| 74 | + fmt.Printf("\nSome required options not defined: %v\n\n", required) |
| 75 | + flagSet.Usage() |
| 76 | + os.Exit(1) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func main() { |
| 81 | + ctx, cancel := context.WithCancel(context.Background()) |
| 82 | + defer cancel() |
| 83 | + db, err := ydb.Open(ctx, dsn, |
| 84 | + ydb.WithOauth2TokenExchangeCredentials( |
| 85 | + credentials.WithTokenEndpoint(tokenEndpoint), |
| 86 | + credentials.WithAudience(audience), |
| 87 | + credentials.WithJWTSubjectToken( |
| 88 | + credentials.WithSigningMethod(jwt.SigningMethodRS256), |
| 89 | + credentials.WithKeyID(keyID), |
| 90 | + credentials.WithRSAPrivateKeyPEMFile(privateKeyFile), |
| 91 | + credentials.WithIssuer(issuer), |
| 92 | + credentials.WithSubject(subject), |
| 93 | + credentials.WithAudience(audience), |
| 94 | + ), |
| 95 | + ), |
| 96 | + ) |
| 97 | + if err != nil { |
| 98 | + panic(err) |
| 99 | + } |
| 100 | + defer func() { _ = db.Close(ctx) }() |
| 101 | + |
| 102 | + whoAmI, err := db.Discovery().WhoAmI(ctx) |
| 103 | + if err != nil { |
| 104 | + panic(err) |
| 105 | + } |
| 106 | + |
| 107 | + fmt.Println(whoAmI.String()) |
| 108 | +} |
0 commit comments