mirror of
				https://github.com/optim-enterprises-bv/vault.git
				synced 2025-10-30 18:17:55 +00:00 
			
		
		
		
	DBPW - Copy newdbplugin package to dbplugin/v5 (#10151)
				
					
				
			This is part 1 of 4 for renaming the `newdbplugin` package. This copies the existing package to the new location but keeps the current one in place so we can migrate the existing references over more easily.
This commit is contained in:
		| @@ -7,14 +7,13 @@ import ( | ||||
| 	"fmt" | ||||
| 	"strings" | ||||
|  | ||||
| 	stdmysql "github.com/go-sql-driver/mysql" | ||||
| 	"github.com/hashicorp/errwrap" | ||||
| 	"github.com/hashicorp/vault/api" | ||||
| 	dbplugin "github.com/hashicorp/vault/sdk/database/dbplugin/v5" | ||||
| 	"github.com/hashicorp/vault/sdk/database/helper/credsutil" | ||||
| 	"github.com/hashicorp/vault/sdk/database/helper/dbutil" | ||||
| 	"github.com/hashicorp/vault/sdk/database/newdbplugin" | ||||
| 	"github.com/hashicorp/vault/sdk/helper/strutil" | ||||
|  | ||||
| 	stdmysql "github.com/go-sql-driver/mysql" | ||||
| ) | ||||
|  | ||||
| const ( | ||||
| @@ -37,7 +36,7 @@ var ( | ||||
| 	LegacyUsernameLen int = 16 | ||||
| ) | ||||
|  | ||||
| var _ newdbplugin.Database = (*MySQL)(nil) | ||||
| var _ dbplugin.Database = (*MySQL)(nil) | ||||
|  | ||||
| type MySQL struct { | ||||
| 	*mySQLConnectionProducer | ||||
| @@ -49,7 +48,7 @@ func New(legacy bool) func() (interface{}, error) { | ||||
| 	return func() (interface{}, error) { | ||||
| 		db := new(legacy) | ||||
| 		// Wrap the plugin with middleware to sanitize errors | ||||
| 		dbType := newdbplugin.NewDatabaseErrorSanitizerMiddleware(db, db.SecretValues) | ||||
| 		dbType := dbplugin.NewDatabaseErrorSanitizerMiddleware(db, db.SecretValues) | ||||
|  | ||||
| 		return dbType, nil | ||||
| 	} | ||||
| @@ -82,7 +81,7 @@ func runCommon(legacy bool, apiTLSConfig *api.TLSConfig) error { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	newdbplugin.Serve(dbType.(newdbplugin.Database), api.VaultPluginTLSProvider(apiTLSConfig)) | ||||
| 	dbplugin.Serve(dbType.(dbplugin.Database), api.VaultPluginTLSProvider(apiTLSConfig)) | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
| @@ -100,25 +99,25 @@ func (m *MySQL) getConnection(ctx context.Context) (*sql.DB, error) { | ||||
| 	return db.(*sql.DB), nil | ||||
| } | ||||
|  | ||||
| func (m *MySQL) Initialize(ctx context.Context, req newdbplugin.InitializeRequest) (newdbplugin.InitializeResponse, error) { | ||||
| func (m *MySQL) Initialize(ctx context.Context, req dbplugin.InitializeRequest) (dbplugin.InitializeResponse, error) { | ||||
| 	err := m.mySQLConnectionProducer.Initialize(ctx, req.Config, req.VerifyConnection) | ||||
| 	if err != nil { | ||||
| 		return newdbplugin.InitializeResponse{}, err | ||||
| 		return dbplugin.InitializeResponse{}, err | ||||
| 	} | ||||
| 	resp := newdbplugin.InitializeResponse{ | ||||
| 	resp := dbplugin.InitializeResponse{ | ||||
| 		Config: req.Config, | ||||
| 	} | ||||
| 	return resp, nil | ||||
| } | ||||
|  | ||||
| func (m *MySQL) NewUser(ctx context.Context, req newdbplugin.NewUserRequest) (newdbplugin.NewUserResponse, error) { | ||||
| func (m *MySQL) NewUser(ctx context.Context, req dbplugin.NewUserRequest) (dbplugin.NewUserResponse, error) { | ||||
| 	if len(req.Statements.Commands) == 0 { | ||||
| 		return newdbplugin.NewUserResponse{}, dbutil.ErrEmptyCreationStatement | ||||
| 		return dbplugin.NewUserResponse{}, dbutil.ErrEmptyCreationStatement | ||||
| 	} | ||||
|  | ||||
| 	username, err := m.generateUsername(req) | ||||
| 	if err != nil { | ||||
| 		return newdbplugin.NewUserResponse{}, err | ||||
| 		return dbplugin.NewUserResponse{}, err | ||||
| 	} | ||||
|  | ||||
| 	password := req.Password | ||||
| @@ -133,16 +132,16 @@ func (m *MySQL) NewUser(ctx context.Context, req newdbplugin.NewUserRequest) (ne | ||||
| 	} | ||||
|  | ||||
| 	if err := m.executePreparedStatementsWithMap(ctx, req.Statements.Commands, queryMap); err != nil { | ||||
| 		return newdbplugin.NewUserResponse{}, err | ||||
| 		return dbplugin.NewUserResponse{}, err | ||||
| 	} | ||||
|  | ||||
| 	resp := newdbplugin.NewUserResponse{ | ||||
| 	resp := dbplugin.NewUserResponse{ | ||||
| 		Username: username, | ||||
| 	} | ||||
| 	return resp, nil | ||||
| } | ||||
|  | ||||
| func (m *MySQL) generateUsername(req newdbplugin.NewUserRequest) (string, error) { | ||||
| func (m *MySQL) generateUsername(req dbplugin.NewUserRequest) (string, error) { | ||||
| 	var dispNameLen, roleNameLen, maxLen int | ||||
|  | ||||
| 	if m.legacy { | ||||
| @@ -167,7 +166,7 @@ func (m *MySQL) generateUsername(req newdbplugin.NewUserRequest) (string, error) | ||||
| 	return username, nil | ||||
| } | ||||
|  | ||||
| func (m *MySQL) DeleteUser(ctx context.Context, req newdbplugin.DeleteUserRequest) (newdbplugin.DeleteUserResponse, error) { | ||||
| func (m *MySQL) DeleteUser(ctx context.Context, req dbplugin.DeleteUserRequest) (dbplugin.DeleteUserResponse, error) { | ||||
| 	// Grab the read lock | ||||
| 	m.Lock() | ||||
| 	defer m.Unlock() | ||||
| @@ -175,7 +174,7 @@ func (m *MySQL) DeleteUser(ctx context.Context, req newdbplugin.DeleteUserReques | ||||
| 	// Get the connection | ||||
| 	db, err := m.getConnection(ctx) | ||||
| 	if err != nil { | ||||
| 		return newdbplugin.DeleteUserResponse{}, err | ||||
| 		return dbplugin.DeleteUserResponse{}, err | ||||
| 	} | ||||
|  | ||||
| 	revocationStmts := req.Statements.Commands | ||||
| @@ -187,7 +186,7 @@ func (m *MySQL) DeleteUser(ctx context.Context, req newdbplugin.DeleteUserReques | ||||
| 	// Start a transaction | ||||
| 	tx, err := db.BeginTx(ctx, nil) | ||||
| 	if err != nil { | ||||
| 		return newdbplugin.DeleteUserResponse{}, err | ||||
| 		return dbplugin.DeleteUserResponse{}, err | ||||
| 	} | ||||
| 	defer tx.Rollback() | ||||
|  | ||||
| @@ -205,31 +204,31 @@ func (m *MySQL) DeleteUser(ctx context.Context, req newdbplugin.DeleteUserReques | ||||
| 			query = strings.Replace(query, "{{username}}", req.Username, -1) | ||||
| 			_, err = tx.ExecContext(ctx, query) | ||||
| 			if err != nil { | ||||
| 				return newdbplugin.DeleteUserResponse{}, err | ||||
| 				return dbplugin.DeleteUserResponse{}, err | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// Commit the transaction | ||||
| 	err = tx.Commit() | ||||
| 	return newdbplugin.DeleteUserResponse{}, err | ||||
| 	return dbplugin.DeleteUserResponse{}, err | ||||
| } | ||||
|  | ||||
| func (m *MySQL) UpdateUser(ctx context.Context, req newdbplugin.UpdateUserRequest) (newdbplugin.UpdateUserResponse, error) { | ||||
| func (m *MySQL) UpdateUser(ctx context.Context, req dbplugin.UpdateUserRequest) (dbplugin.UpdateUserResponse, error) { | ||||
| 	if req.Password == nil && req.Expiration == nil { | ||||
| 		return newdbplugin.UpdateUserResponse{}, fmt.Errorf("no change requested") | ||||
| 		return dbplugin.UpdateUserResponse{}, fmt.Errorf("no change requested") | ||||
| 	} | ||||
|  | ||||
| 	if req.Password != nil { | ||||
| 		err := m.changeUserPassword(ctx, req.Username, req.Password.NewPassword, req.Password.Statements.Commands) | ||||
| 		if err != nil { | ||||
| 			return newdbplugin.UpdateUserResponse{}, fmt.Errorf("failed to change password: %w", err) | ||||
| 			return dbplugin.UpdateUserResponse{}, fmt.Errorf("failed to change password: %w", err) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// Expiration change/update is currently a no-op | ||||
|  | ||||
| 	return newdbplugin.UpdateUserResponse{}, nil | ||||
| 	return dbplugin.UpdateUserResponse{}, nil | ||||
| } | ||||
|  | ||||
| func (m *MySQL) changeUserPassword(ctx context.Context, username, password string, rotateStatements []string) error { | ||||
|   | ||||
| @@ -9,14 +9,13 @@ import ( | ||||
|  | ||||
| 	stdmysql "github.com/go-sql-driver/mysql" | ||||
| 	mysqlhelper "github.com/hashicorp/vault/helper/testhelpers/mysql" | ||||
|  | ||||
| 	dbplugin "github.com/hashicorp/vault/sdk/database/dbplugin/v5" | ||||
| 	"github.com/hashicorp/vault/sdk/database/helper/credsutil" | ||||
| 	"github.com/hashicorp/vault/sdk/database/helper/dbutil" | ||||
| 	"github.com/hashicorp/vault/sdk/database/newdbplugin" | ||||
| 	"github.com/hashicorp/vault/sdk/helper/strutil" | ||||
| ) | ||||
|  | ||||
| var _ newdbplugin.Database = (*MySQL)(nil) | ||||
| var _ dbplugin.Database = (*MySQL)(nil) | ||||
|  | ||||
| func TestMySQL_Initialize(t *testing.T) { | ||||
| 	cleanup, connURL := mysqlhelper.PrepareTestContainer(t, false, "secret") | ||||
| @@ -26,7 +25,7 @@ func TestMySQL_Initialize(t *testing.T) { | ||||
| 		"connection_url": connURL, | ||||
| 	} | ||||
|  | ||||
| 	initReq := newdbplugin.InitializeRequest{ | ||||
| 	initReq := dbplugin.InitializeRequest{ | ||||
| 		Config:           connectionDetails, | ||||
| 		VerifyConnection: true, | ||||
| 	} | ||||
| @@ -52,7 +51,7 @@ func TestMySQL_Initialize(t *testing.T) { | ||||
| 		"max_open_connections": "5", | ||||
| 	} | ||||
|  | ||||
| 	initReq = newdbplugin.InitializeRequest{ | ||||
| 	initReq = dbplugin.InitializeRequest{ | ||||
| 		Config:           connectionDetails, | ||||
| 		VerifyConnection: true, | ||||
| 	} | ||||
| @@ -73,12 +72,12 @@ func TestMySQL_CreateUser(t *testing.T) { | ||||
| 			t.Fatalf("unable to generate password: %s", err) | ||||
| 		} | ||||
|  | ||||
| 		createReq := newdbplugin.NewUserRequest{ | ||||
| 			UsernameConfig: newdbplugin.UsernameMetadata{ | ||||
| 		createReq := dbplugin.NewUserRequest{ | ||||
| 			UsernameConfig: dbplugin.UsernameMetadata{ | ||||
| 				DisplayName: "test", | ||||
| 				RoleName:    "test", | ||||
| 			}, | ||||
| 			Statements: newdbplugin.Statements{ | ||||
| 			Statements: dbplugin.Statements{ | ||||
| 				Commands: []string{}, | ||||
| 			}, | ||||
| 			Password:   password, | ||||
| @@ -103,7 +102,7 @@ func TestMySQL_CreateUser(t *testing.T) { | ||||
| 			"connection_url": connURL, | ||||
| 		} | ||||
|  | ||||
| 		initReq := newdbplugin.InitializeRequest{ | ||||
| 		initReq := dbplugin.InitializeRequest{ | ||||
| 			Config:           connectionDetails, | ||||
| 			VerifyConnection: true, | ||||
| 		} | ||||
| @@ -126,7 +125,7 @@ func TestMySQL_CreateUser(t *testing.T) { | ||||
| 			"connection_url": connURL, | ||||
| 		} | ||||
|  | ||||
| 		initReq := newdbplugin.InitializeRequest{ | ||||
| 		initReq := dbplugin.InitializeRequest{ | ||||
| 			Config:           connectionDetails, | ||||
| 			VerifyConnection: true, | ||||
| 		} | ||||
| @@ -188,12 +187,12 @@ func testCreateUser(t *testing.T, db *MySQL, connURL string) { | ||||
| 				t.Fatalf("unable to generate password: %s", err) | ||||
| 			} | ||||
|  | ||||
| 			createReq := newdbplugin.NewUserRequest{ | ||||
| 				UsernameConfig: newdbplugin.UsernameMetadata{ | ||||
| 			createReq := dbplugin.NewUserRequest{ | ||||
| 				UsernameConfig: dbplugin.UsernameMetadata{ | ||||
| 					DisplayName: "test", | ||||
| 					RoleName:    "test", | ||||
| 				}, | ||||
| 				Statements: newdbplugin.Statements{ | ||||
| 				Statements: dbplugin.Statements{ | ||||
| 					Commands: test.createStmts, | ||||
| 				}, | ||||
| 				Password:   password, | ||||
| @@ -256,7 +255,7 @@ func TestMySQL_RotateRootCredentials(t *testing.T) { | ||||
| 			ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||||
| 			defer cancel() | ||||
|  | ||||
| 			initReq := newdbplugin.InitializeRequest{ | ||||
| 			initReq := dbplugin.InitializeRequest{ | ||||
| 				Config:           connectionDetails, | ||||
| 				VerifyConnection: true, | ||||
| 			} | ||||
| @@ -271,11 +270,11 @@ func TestMySQL_RotateRootCredentials(t *testing.T) { | ||||
| 				t.Fatal("Database should be initialized") | ||||
| 			} | ||||
|  | ||||
| 			updateReq := newdbplugin.UpdateUserRequest{ | ||||
| 			updateReq := dbplugin.UpdateUserRequest{ | ||||
| 				Username: "root", | ||||
| 				Password: &newdbplugin.ChangePassword{ | ||||
| 				Password: &dbplugin.ChangePassword{ | ||||
| 					NewPassword: "different_sercret", | ||||
| 					Statements: newdbplugin.Statements{ | ||||
| 					Statements: dbplugin.Statements{ | ||||
| 						Commands: test.statements, | ||||
| 					}, | ||||
| 				}, | ||||
| @@ -331,7 +330,7 @@ func TestMySQL_DeleteUser(t *testing.T) { | ||||
| 		"connection_url": connURL, | ||||
| 	} | ||||
|  | ||||
| 	initReq := newdbplugin.InitializeRequest{ | ||||
| 	initReq := dbplugin.InitializeRequest{ | ||||
| 		Config:           connectionDetails, | ||||
| 		VerifyConnection: true, | ||||
| 	} | ||||
| @@ -349,12 +348,12 @@ func TestMySQL_DeleteUser(t *testing.T) { | ||||
| 				t.Fatalf("unable to generate password: %s", err) | ||||
| 			} | ||||
|  | ||||
| 			createReq := newdbplugin.NewUserRequest{ | ||||
| 				UsernameConfig: newdbplugin.UsernameMetadata{ | ||||
| 			createReq := dbplugin.NewUserRequest{ | ||||
| 				UsernameConfig: dbplugin.UsernameMetadata{ | ||||
| 					DisplayName: "test", | ||||
| 					RoleName:    "test", | ||||
| 				}, | ||||
| 				Statements: newdbplugin.Statements{ | ||||
| 				Statements: dbplugin.Statements{ | ||||
| 					Commands: []string{` | ||||
| 						CREATE USER '{{name}}'@'%' IDENTIFIED BY '{{password}}'; | ||||
| 						GRANT SELECT ON *.* TO '{{name}}'@'%';`, | ||||
| @@ -377,9 +376,9 @@ func TestMySQL_DeleteUser(t *testing.T) { | ||||
| 				t.Fatalf("Could not connect with new credentials: %s", err) | ||||
| 			} | ||||
|  | ||||
| 			deleteReq := newdbplugin.DeleteUserRequest{ | ||||
| 			deleteReq := dbplugin.DeleteUserRequest{ | ||||
| 				Username: userResp.Username, | ||||
| 				Statements: newdbplugin.Statements{ | ||||
| 				Statements: dbplugin.Statements{ | ||||
| 					Commands: test.revokeStmts, | ||||
| 				}, | ||||
| 			} | ||||
| @@ -436,7 +435,7 @@ func TestMySQL_UpdateUser(t *testing.T) { | ||||
| 				"connection_url": connURL, | ||||
| 			} | ||||
|  | ||||
| 			initReq := newdbplugin.InitializeRequest{ | ||||
| 			initReq := dbplugin.InitializeRequest{ | ||||
| 				Config:           connectionDetails, | ||||
| 				VerifyConnection: true, | ||||
| 			} | ||||
| @@ -456,11 +455,11 @@ func TestMySQL_UpdateUser(t *testing.T) { | ||||
| 				t.Fatalf("unable to generate password: %s", err) | ||||
| 			} | ||||
|  | ||||
| 			updateReq := newdbplugin.UpdateUserRequest{ | ||||
| 			updateReq := dbplugin.UpdateUserRequest{ | ||||
| 				Username: dbUser, | ||||
| 				Password: &newdbplugin.ChangePassword{ | ||||
| 				Password: &dbplugin.ChangePassword{ | ||||
| 					NewPassword: newPassword, | ||||
| 					Statements: newdbplugin.Statements{ | ||||
| 					Statements: dbplugin.Statements{ | ||||
| 						Commands: test.rotateStmts, | ||||
| 					}, | ||||
| 				}, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Michael Golowka
					Michael Golowka