| 1 |
// Copyright 2023 The Archivista Contributors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package sqlstore |
| 16 |
|
| 17 |
import ( |
| 18 |
"database/sql" |
| 19 |
"fmt" |
| 20 |
"strings" |
| 21 |
"time" |
| 22 |
|
| 23 |
"ariga.io/sqlcomment" |
| 24 |
"entgo.io/ent/dialect" |
| 25 |
entsql "entgo.io/ent/dialect/sql" |
| 26 |
"github.com/in-toto/archivista/ent" |
| 27 |
"github.com/jkjell/go-db-credential-refresh/driver" |
| 28 |
"github.com/jkjell/go-db-credential-refresh/store/static" |
| 29 |
) |
| 30 |
|
| 31 |
type ClientOption func(*clientOptions) |
| 32 |
|
| 33 |
type clientOptions struct { |
| 34 |
maxIdleConns int |
| 35 |
maxOpenConns int |
| 36 |
connMaxLifetime time.Duration |
| 37 |
} |
| 38 |
|
| 39 |
// Configures a client with the specified max idle connections. Default is 10 connections |
| 40 |
func ClientWithMaxIdleConns(maxIdleConns int) ClientOption { |
| 41 |
return func(co *clientOptions) { |
| 42 |
co.maxIdleConns = maxIdleConns |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
// Configures a client with the specified max open connections. Default is 100 connections |
| 47 |
func ClientWithMaxOpenConns(maxOpenConns int) ClientOption { |
| 48 |
return func(co *clientOptions) { |
| 49 |
co.maxOpenConns = maxOpenConns |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// Congiures a client with the specified max connection lifetime. Default is 3 minutes |
| 54 |
func ClientWithConnMaxLifetime(connMaxLifetime time.Duration) ClientOption { |
| 55 |
return func(co *clientOptions) { |
| 56 |
co.connMaxLifetime = connMaxLifetime |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
// NewEntClient creates an ent client for use in the sqlmetadata store. |
| 61 |
// Valid backends are MYSQL and PSQL. |
| 62 |
func NewEntClient(sqlBackend string, connectionString string, opts ...ClientOption) (*ent.Client, error) { |
| 63 |
clientOpts := &clientOptions{ |
| 64 |
maxIdleConns: 10, |
| 65 |
maxOpenConns: 100, |
| 66 |
connMaxLifetime: 3 * time.Minute, |
| 67 |
} |
| 68 |
|
| 69 |
for _, opt := range opts { |
| 70 |
opt(clientOpts) |
| 71 |
} |
| 72 |
|
| 73 |
var s driver.Store |
| 74 |
var dc *driver.Config |
| 75 |
var entDialect, driverName, user, password string |
| 76 |
|
| 77 |
upperSqlBackend := strings.ToUpper(sqlBackend) |
| 78 |
if strings.HasPrefix(upperSqlBackend, "MYSQL") { |
| 79 |
var err error |
| 80 |
dc, user, password, err = ConfigFromMySQL(connectionString) |
| 81 |
if err != nil { |
| 82 |
return nil, fmt.Errorf("could not get driver config from mysql connection string: %w", err) |
| 83 |
} |
| 84 |
|
| 85 |
entDialect = dialect.MySQL |
| 86 |
driverName = "mysql" |
| 87 |
} else if strings.HasPrefix(upperSqlBackend, "PSQL") { |
| 88 |
var err error |
| 89 |
dc, user, password, err = ConfigFromPostgres(connectionString) |
| 90 |
if err != nil { |
| 91 |
return nil, fmt.Errorf("could not get driver config from postgres connection string: %w", err) |
| 92 |
} |
| 93 |
|
| 94 |
entDialect = dialect.Postgres |
| 95 |
driverName = "pgx" |
| 96 |
} else { |
| 97 |
return nil, fmt.Errorf("unknown sql backend: %s", sqlBackend) |
| 98 |
} |
| 99 |
|
| 100 |
// if upperSqlBackend ends with _RDS_IAM, then use awsrds store |
| 101 |
if strings.HasSuffix(upperSqlBackend, "_RDS_IAM") { |
| 102 |
var err error |
| 103 |
s, err = AWSRDSStoreFromDriverConfig(dc, user) |
| 104 |
if err != nil { |
| 105 |
return nil, fmt.Errorf("could not create credentials refresh store: %w", err) |
| 106 |
} |
| 107 |
} else { |
| 108 |
s = static.NewStaticStore(user, password) |
| 109 |
dc.Retries = 0 // no retries for static credentials |
| 110 |
} |
| 111 |
|
| 112 |
c, err := driver.NewConnector(s, driverName, dc) |
| 113 |
if err != nil { |
| 114 |
return nil, fmt.Errorf("could not create connector: %w", err) |
| 115 |
} |
| 116 |
|
| 117 |
db := sql.OpenDB(c) |
| 118 |
db.SetMaxIdleConns(clientOpts.maxIdleConns) |
| 119 |
db.SetMaxOpenConns(clientOpts.maxOpenConns) |
| 120 |
db.SetConnMaxLifetime(clientOpts.connMaxLifetime) |
| 121 |
|
| 122 |
drv := entsql.OpenDB(entDialect, db) |
| 123 |
sqlcommentDrv := sqlcomment.NewDriver(drv, |
| 124 |
sqlcomment.WithDriverVerTag(), |
| 125 |
sqlcomment.WithTags(sqlcomment.Tags{ |
| 126 |
sqlcomment.KeyApplication: "archivista", |
| 127 |
sqlcomment.KeyFramework: "net/http", |
| 128 |
}), |
| 129 |
) |
| 130 |
|
| 131 |
client := ent.NewClient(ent.Driver(sqlcommentDrv)) |
| 132 |
return client, nil |
| 133 |
} |