Skip to content

Commit 07800a3

Browse files
author
TJ Zhang
committed
updates
Signed-off-by: TJ Zhang <[email protected]>
1 parent 18cb55c commit 07800a3

7 files changed

+134
-39
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* Go: Add `GeoAdd` and the Geospatial interface ([#3366](https://github.com/valkey-io/valkey-glide/pull/3366))
2020
* Go: Add `FLUSHALL` ([#3117](https://github.com/valkey-io/valkey-glide/pull/3117))
2121
* Go: Add `FLUSHDB` ([#3117](https://github.com/valkey-io/valkey-glide/pull/3117))
22+
* Go: Add password update api ([#3346](https://github.com/valkey-io/valkey-glide/pull/3346))
2223

2324
#### Breaking Changes
2425

go/api/base_client.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -379,18 +379,11 @@ func (client *baseClient) UpdateConnectionPassword(password string, immediateAut
379379
// This method updates the client's internal password configuration and does not perform
380380
// password rotation on the server side.
381381
//
382-
// Parameters:
383-
//
384-
// immediateAuth - immediateAuth A boolean flag. If true, the client will
385-
// authenticate immediately with the new password against all connections, Using AUTH
386-
// command. If password supplied is an empty string, the client will not perform auth and a warning
387-
// will be returned. The default is `false`.
388-
//
389382
// Return value:
390383
//
391384
// `"OK"` response on success.
392-
func (client *baseClient) ResetConnectionPassword(immediateAuth bool) (Result[string], error) {
393-
return client.submitConnectionPasswordUpdate("", immediateAuth)
385+
func (client *baseClient) ResetConnectionPassword() (Result[string], error) {
386+
return client.submitConnectionPasswordUpdate("", false)
394387
}
395388

396389
// Set the given key with the given value. The return value is a response from Valkey containing the string "OK".

go/api/generic_base_commands.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,5 @@ type GenericBaseCommands interface {
8484

8585
UpdateConnectionPassword(password string, immediateAuth bool) (Result[string], error)
8686

87-
ResetConnectionPassword(immediateAuth bool) (Result[string], error)
87+
ResetConnectionPassword() (Result[string], error)
8888
}

go/api/update_connection_password_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func ExampleGlideClient_UpdateConnectionPassword() {
1919

2020
func ExampleGlideClient_ResetConnectionPassword() {
2121
var client *GlideClient = getExampleGlideClient() // example helper function
22-
response, err := client.ResetConnectionPassword(false)
22+
response, err := client.ResetConnectionPassword()
2323
if err != nil {
2424
fmt.Println("Glide example failed with an error: ", err)
2525
}
@@ -41,7 +41,7 @@ func ExampleGlideClusterClient_UpdateConnectionPassword() {
4141

4242
func ExampleGlideClusterClient_ResetConnectionPassword() {
4343
var client *GlideClusterClient = getExampleGlideClusterClient() // example helper function
44-
response, err := client.ResetConnectionPassword(false)
44+
response, err := client.ResetConnectionPassword()
4545
if err != nil {
4646
fmt.Println("Glide example failed with an error: ", err)
4747
}

go/integTest/cluster_commands_test.go

+22-23
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ func (suite *GlideTestSuite) TestFlushDBWithOptions_AsyncMode() {
973973
assert.Empty(suite.T(), val.Value())
974974
}
975975

976-
func (suite *GlideTestSuite) TestUpdateConnectionPassword() {
976+
func (suite *GlideTestSuite) TestUpdateConnectionPasswordCluster() {
977977
// Create admin client
978978
adminClient := suite.defaultClusterClient()
979979
defer adminClient.Close()
@@ -1008,35 +1008,34 @@ func (suite *GlideTestSuite) TestUpdateConnectionPassword() {
10081008
_, err = testClient.Info()
10091009
assert.NoError(suite.T(), err)
10101010

1011-
// Cleanup: Reset password
1011+
// test reset connection password
1012+
_, err = testClient.ResetConnectionPassword()
1013+
assert.NoError(suite.T(), err)
1014+
1015+
// Cleanup: config set reset password
10121016
_, err = adminClient.CustomCommand([]string{"CONFIG", "SET", "requirepass", ""})
10131017
assert.NoError(suite.T(), err)
10141018
}
10151019

1016-
func (suite *GlideTestSuite) TestUpdateConnectionPassword_InvalidParameters() {
1020+
func (suite *GlideTestSuite) TestUpdateConnectionPasswordCluster_InvalidParameters() {
10171021
// Create test client
1018-
testClient := suite.defaultClient()
1022+
testClient := suite.defaultClusterClient()
10191023
defer testClient.Close()
10201024

10211025
// Test empty password
10221026
_, err := testClient.UpdateConnectionPassword("", true)
10231027
assert.NotNil(suite.T(), err)
10241028
assert.IsType(suite.T(), &errors.RequestError{}, err)
1025-
1026-
// Test no password (assuming the method exists with this signature)
1027-
_, err = testClient.UpdateConnectionPassword("", true)
1028-
assert.NotNil(suite.T(), err)
1029-
assert.IsType(suite.T(), &errors.RequestError{}, err)
10301029
}
10311030

1032-
func (suite *GlideTestSuite) TestUpdateConnectionPassword_NoServerAuth() {
1031+
func (suite *GlideTestSuite) TestUpdateConnectionPasswordCluster_NoServerAuth() {
10331032
// Create test client
1034-
testClient := suite.defaultClient()
1033+
testClient := suite.defaultClusterClient()
10351034
defer testClient.Close()
10361035

10371036
// Validate that we can use the client
10381037
_, err := testClient.Info()
1039-
assert.Nil(suite.T(), err)
1038+
assert.NoError(suite.T(), err)
10401039

10411040
// Test immediate re-authentication fails when no server password is set
10421041
pwd := uuid.NewString()
@@ -1045,9 +1044,9 @@ func (suite *GlideTestSuite) TestUpdateConnectionPassword_NoServerAuth() {
10451044
assert.IsType(suite.T(), &errors.RequestError{}, err)
10461045
}
10471046

1048-
func (suite *GlideTestSuite) TestUpdateConnectionPassword_LongPassword() {
1047+
func (suite *GlideTestSuite) TestUpdateConnectionPasswordCluster_LongPassword() {
10491048
// Create test client
1050-
testClient := suite.defaultClient()
1049+
testClient := suite.defaultClusterClient()
10511050
defer testClient.Close()
10521051

10531052
// Generate long random password (1000 chars)
@@ -1059,32 +1058,32 @@ func (suite *GlideTestSuite) TestUpdateConnectionPassword_LongPassword() {
10591058

10601059
// Validate that we can use the client
10611060
_, err := testClient.Info()
1062-
assert.Nil(suite.T(), err)
1061+
assert.NoError(suite.T(), err)
10631062

10641063
// Test replacing connection password with a long password string
10651064
_, err = testClient.UpdateConnectionPassword(string(pwd), false)
1066-
assert.Nil(suite.T(), err)
1065+
assert.NoError(suite.T(), err)
10671066
}
10681067

1069-
func (suite *GlideTestSuite) TestUpdateConnectionPassword_ImmediateAuthWrongPassword() {
1068+
func (suite *GlideTestSuite) TestUpdateConnectionPasswordCluster_ImmediateAuthWrongPassword() {
10701069
// Create admin client
1071-
adminClient := suite.defaultClient()
1070+
adminClient := suite.defaultClusterClient()
10721071
defer adminClient.Close()
10731072

10741073
// Create test client
1075-
testClient := suite.defaultClient()
1074+
testClient := suite.defaultClusterClient()
10761075
defer testClient.Close()
10771076

10781077
pwd := uuid.NewString()
10791078
notThePwd := uuid.NewString()
10801079

10811080
// Validate that we can use the client
10821081
_, err := testClient.Info()
1083-
assert.Nil(suite.T(), err)
1082+
assert.NoError(suite.T(), err)
10841083

10851084
// Set the password to something else
10861085
_, err = adminClient.CustomCommand([]string{"CONFIG", "SET", "requirepass", notThePwd})
1087-
assert.Nil(suite.T(), err)
1086+
assert.NoError(suite.T(), err)
10881087

10891088
// Test that re-authentication fails when using wrong password
10901089
_, err = testClient.UpdateConnectionPassword(pwd, true)
@@ -1093,9 +1092,9 @@ func (suite *GlideTestSuite) TestUpdateConnectionPassword_ImmediateAuthWrongPass
10931092

10941093
// But using correct password returns OK
10951094
_, err = testClient.UpdateConnectionPassword(notThePwd, true)
1096-
assert.Nil(suite.T(), err)
1095+
assert.NoError(suite.T(), err)
10971096

10981097
// Cleanup: Reset password
10991098
_, err = adminClient.CustomCommand([]string{"CONFIG", "SET", "requirepass", ""})
1100-
assert.Nil(suite.T(), err)
1099+
assert.NoError(suite.T(), err)
11011100
}

go/integTest/standalone_commands_test.go

+88
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package integTest
44

55
import (
66
"fmt"
7+
"math/rand"
78
"strconv"
89
"strings"
910
"time"
@@ -701,3 +702,90 @@ func (suite *GlideTestSuite) TestFlushDBWithOptions_ClosedClient() {
701702
assert.Equal(suite.T(), "", result)
702703
assert.IsType(suite.T(), &errors.ClosingError{}, err)
703704
}
705+
706+
func (suite *GlideTestSuite) TestUpdateConnectionPasswordAuthNonValidPass() {
707+
// Create test client
708+
testClient := suite.defaultClient()
709+
defer testClient.Close()
710+
711+
// Test empty password
712+
_, err := testClient.UpdateConnectionPassword("", true)
713+
assert.NotNil(suite.T(), err)
714+
assert.IsType(suite.T(), &errors.RequestError{}, err)
715+
716+
// Test with no password parameter
717+
_, err = testClient.UpdateConnectionPassword("", true)
718+
assert.NotNil(suite.T(), err)
719+
assert.IsType(suite.T(), &errors.RequestError{}, err)
720+
}
721+
722+
func (suite *GlideTestSuite) TestUpdateConnectionPassword_NoServerAuth() {
723+
// Create test client
724+
testClient := suite.defaultClient()
725+
defer testClient.Close()
726+
727+
// Validate that we can use the client
728+
_, err := testClient.Info()
729+
assert.Nil(suite.T(), err)
730+
731+
// Test immediate re-authentication fails when no server password is set
732+
pwd := uuid.NewString()
733+
_, err = testClient.UpdateConnectionPassword(pwd, true)
734+
assert.NotNil(suite.T(), err)
735+
assert.IsType(suite.T(), &errors.RequestError{}, err)
736+
}
737+
738+
func (suite *GlideTestSuite) TestUpdateConnectionPassword_LongPassword() {
739+
// Create test client
740+
testClient := suite.defaultClient()
741+
defer testClient.Close()
742+
743+
// Generate long random password (1000 chars)
744+
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
745+
pwd := make([]byte, 1000)
746+
for i := range pwd {
747+
pwd[i] = letters[rand.Intn(len(letters))]
748+
}
749+
750+
// Validate that we can use the client
751+
_, err := testClient.Info()
752+
assert.NoError(suite.T(), err)
753+
754+
// Test replacing connection password with a long password string
755+
_, err = testClient.UpdateConnectionPassword(string(pwd), false)
756+
assert.NoError(suite.T(), err)
757+
}
758+
759+
func (suite *GlideTestSuite) TestUpdateConnectionPassword_ImmediateAuthWrongPassword() {
760+
// Create admin client
761+
adminClient := suite.defaultClient()
762+
defer adminClient.Close()
763+
764+
// Create test client
765+
testClient := suite.defaultClient()
766+
defer testClient.Close()
767+
768+
pwd := uuid.NewString()
769+
notThePwd := uuid.NewString()
770+
771+
// Validate that we can use the client
772+
_, err := testClient.Info()
773+
assert.Nil(suite.T(), err)
774+
775+
// Set the password to something else
776+
_, err = adminClient.ConfigSet(map[string]string{"requirepass": notThePwd})
777+
assert.Nil(suite.T(), err)
778+
779+
// Test that re-authentication fails when using wrong password
780+
_, err = testClient.UpdateConnectionPassword(pwd, true)
781+
assert.NotNil(suite.T(), err)
782+
assert.IsType(suite.T(), &errors.RequestError{}, err)
783+
784+
// But using correct password returns OK
785+
_, err = testClient.UpdateConnectionPassword(notThePwd, true)
786+
assert.NoError(suite.T(), err)
787+
788+
// Cleanup: Reset password
789+
_, err = adminClient.ConfigSet(map[string]string{"requirepass": ""})
790+
assert.NoError(suite.T(), err)
791+
}

go/src/lib.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,21 @@ pub unsafe extern "C" fn request_cluster_scan(
851851
});
852852
}
853853

854+
/// CGO method which allows the Go client to request an update to the connection password.
855+
///
856+
/// `client_adapter_ptr` is a pointer to a valid `GlideClusterClient` returned in the `ConnectionResponse` from [`create_client`].
857+
/// `channel` is a pointer to a valid payload buffer which is created in the Go client.
858+
/// `password` is a pointer to C string representation of the password passed in Go.
859+
/// `immediate_auth` is a boolean flag to indicate if the password should be updated immediately.
860+
/// `success_callback` is the callback that will be called when a command succeeds.
861+
/// `failure_callback` is the callback that will be called when a command fails.
862+
///
863+
/// # Safety
864+
///
865+
/// * `client_adapter_ptr` must be obtained from the `ConnectionResponse` returned from [`create_client`].
866+
/// * `client_adapter_ptr` must be valid until `close_client` is called.
867+
/// * `channel` must be valid until it is passed in a call to [`free_command_response`].
868+
/// * Both the `success_callback` and `failure_callback` function pointers need to live while the client is open/active. The caller is responsible for freeing both callbacks.
854869
#[no_mangle]
855870
pub unsafe extern "C" fn update_connection_password(
856871
client_adapter_ptr: *const c_void,
@@ -865,16 +880,15 @@ pub unsafe extern "C" fn update_connection_password(
865880
let ptr_address = client_adapter_ptr as usize;
866881

867882
let mut client_clone = client_adapter.client.clone();
868-
883+
869884
// argument conversion to be used in the async block
870885
let password = unsafe { CStr::from_ptr(password).to_str().unwrap() };
871886
let password_option = if password.is_empty() {
872887
None
873888
} else {
874889
Some(password.to_string())
875890
};
876-
let immediate_auth = immediate_auth;
877-
891+
878892
client_adapter.runtime.spawn(async move {
879893
let result = client_clone
880894
.update_connection_password(password_option, immediate_auth)
@@ -912,5 +926,5 @@ pub unsafe extern "C" fn update_connection_password(
912926
}
913927
};
914928
}
915-
});
929+
});
916930
}

0 commit comments

Comments
 (0)