-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
164 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright © by Jeff Foley 2017-2024. All rights reserved. | ||
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package assetdb | ||
|
||
import ( | ||
"embed" | ||
"fmt" | ||
"math/rand" | ||
|
||
pgmigrations "github.com/owasp-amass/asset-db/migrations/postgres" | ||
sqlitemigrations "github.com/owasp-amass/asset-db/migrations/sqlite3" | ||
"github.com/owasp-amass/asset-db/repository" | ||
"github.com/owasp-amass/asset-db/repository/sqlrepo" | ||
migrate "github.com/rubenv/sql-migrate" | ||
"gorm.io/driver/postgres" | ||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
) | ||
|
||
// New creates a new assetDB instance. | ||
// It initializes the asset database with the specified database type and DSN. | ||
func New(dbtype, dsn string) *AssetDB { | ||
if dbtype == sqlrepo.SQLiteMemory { | ||
dsn = fmt.Sprintf("file:sqlite%d?mode=memory&cache=shared", rand.Int31n(1000)) | ||
} | ||
|
||
if db, err := repository.New(dbtype, dsn); err == nil && db != nil { | ||
if err := migrateDatabase(dbtype, dsn); err == nil { | ||
return &AssetDB{ | ||
repository: db, | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Close will close the assetdb and return any errors. | ||
func (as *AssetDB) Close() error { | ||
return as.repository.Close() | ||
} | ||
|
||
// GetDBType returns the type of the underlying database. | ||
func (as *AssetDB) GetDBType() string { | ||
return as.repository.GetDBType() | ||
} | ||
|
||
func migrateDatabase(dbtype, dsn string) error { | ||
var name string | ||
var fs embed.FS | ||
var database gorm.Dialector | ||
|
||
switch dbtype { | ||
case sqlrepo.SQLite: | ||
fallthrough | ||
case sqlrepo.SQLiteMemory: | ||
name = "sqlite3" | ||
fs = sqlitemigrations.Migrations() | ||
database = sqlite.Open(dsn) | ||
case sqlrepo.Postgres: | ||
name = "postgres" | ||
fs = pgmigrations.Migrations() | ||
database = postgres.Open(dsn) | ||
} | ||
|
||
sql, err := gorm.Open(database, &gorm.Config{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
migrationsSource := migrate.EmbedFileSystemMigrationSource{ | ||
FileSystem: fs, | ||
Root: "/", | ||
} | ||
|
||
sqlDb, err := sql.DB() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = migrate.Exec(sqlDb, name, migrationsSource, migrate.Up) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright © by Jeff Foley 2017-2024. All rights reserved. | ||
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package sqlrepo | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/glebarez/sqlite" | ||
"gorm.io/driver/postgres" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/logger" | ||
) | ||
|
||
const ( | ||
Postgres string = "postgres" | ||
SQLite string = "sqlite" | ||
SQLiteMemory string = "sqlite_memory" | ||
) | ||
|
||
// sqlRepository is a repository implementation using GORM as the underlying ORM. | ||
type sqlRepository struct { | ||
db *gorm.DB | ||
dbtype string | ||
} | ||
|
||
// New creates a new instance of the asset database repository. | ||
func New(dbtype, dsn string) (*sqlRepository, error) { | ||
db, err := newDatabase(dbtype, dsn) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &sqlRepository{ | ||
db: db, | ||
dbtype: dbtype, | ||
}, nil | ||
} | ||
|
||
// newDatabase creates a new GORM database connection based on the provided database type and data source name (dsn). | ||
func newDatabase(dbtype, dsn string) (*gorm.DB, error) { | ||
switch dbtype { | ||
case Postgres: | ||
return postgresDatabase(dsn) | ||
case SQLite: | ||
fallthrough | ||
case SQLiteMemory: | ||
return sqliteDatabase(dsn) | ||
} | ||
return nil, errors.New("unknown DB type") | ||
} | ||
|
||
// postgresDatabase creates a new PostgreSQL database connection using the provided data source name (dsn). | ||
func postgresDatabase(dsn string) (*gorm.DB, error) { | ||
return gorm.Open(postgres.Open(dsn), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)}) | ||
} | ||
|
||
// sqliteDatabase creates a new SQLite database connection using the provided data source name (dsn). | ||
func sqliteDatabase(dsn string) (*gorm.DB, error) { | ||
return gorm.Open(sqlite.Open(dsn), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)}) | ||
} | ||
|
||
// Close implements the Repository interface. | ||
func (sql *sqlRepository) Close() error { | ||
if db, err := sql.db.DB(); err == nil { | ||
return db.Close() | ||
} | ||
return errors.New("failed to obtain access to the database handle") | ||
} | ||
|
||
// GetDBType returns the type of the database. | ||
func (sql *sqlRepository) GetDBType() string { | ||
return string(sql.dbtype) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters