1
1
package coderd_test
2
2
3
3
import (
4
+ "database/sql"
4
5
"fmt"
5
6
"net/http"
6
7
"net/http/httptest"
@@ -17,6 +18,7 @@ import (
17
18
"github.com/coder/coder/v2/buildinfo"
18
19
"github.com/coder/coder/v2/coderd/coderdtest"
19
20
"github.com/coder/coder/v2/coderd/database"
21
+ "github.com/coder/coder/v2/coderd/database/dbgen"
20
22
"github.com/coder/coder/v2/coderd/database/dbtestutil"
21
23
"github.com/coder/coder/v2/coderd/database/dbtime"
22
24
"github.com/coder/coder/v2/coderd/workspaceapps"
@@ -887,3 +889,118 @@ func TestReconnectingPTYSignedToken(t *testing.T) {
887
889
// validate it here.
888
890
})
889
891
}
892
+
893
+ func TestGetCryptoKeys (t * testing.T ) {
894
+ t .Parallel ()
895
+
896
+ t .Run ("OK" , func (t * testing.T ) {
897
+ t .Parallel ()
898
+
899
+ ctx := testutil .Context (t , testutil .WaitMedium )
900
+ db , pubsub := dbtestutil .NewDB (t )
901
+ cclient , _ , api , _ := coderdenttest .NewWithAPI (t , & coderdenttest.Options {
902
+ Options : & coderdtest.Options {
903
+ Database : db ,
904
+ Pubsub : pubsub ,
905
+ IncludeProvisionerDaemon : true ,
906
+ },
907
+ LicenseOptions : & coderdenttest.LicenseOptions {
908
+ Features : license.Features {
909
+ codersdk .FeatureWorkspaceProxy : 1 ,
910
+ },
911
+ },
912
+ })
913
+
914
+ now := time .Now ().UTC ()
915
+
916
+ expectedKey1 := dbgen .CryptoKey (t , db , database.CryptoKey {
917
+ Feature : database .CryptoKeyFeatureWorkspaceApps ,
918
+ StartsAt : now .Add (- time .Hour ),
919
+ Sequence : 2 ,
920
+ })
921
+ key1 := fromDBCryptoKeys (expectedKey1 )
922
+
923
+ expectedKey2 := dbgen .CryptoKey (t , db , database.CryptoKey {
924
+ Feature : database .CryptoKeyFeatureWorkspaceApps ,
925
+ StartsAt : now ,
926
+ Sequence : 3 ,
927
+ })
928
+ key2 := fromDBCryptoKeys (expectedKey2 )
929
+
930
+ // Create a deleted key.
931
+ _ = dbgen .CryptoKey (t , db , database.CryptoKey {
932
+ Feature : database .CryptoKeyFeatureWorkspaceApps ,
933
+ StartsAt : now .Add (- time .Hour ),
934
+ Secret : sql.NullString {
935
+ String : "secret1" ,
936
+ Valid : false ,
937
+ },
938
+ Sequence : 1 ,
939
+ })
940
+
941
+ // Create a key with different features.
942
+ _ = dbgen .CryptoKey (t , db , database.CryptoKey {
943
+ Feature : database .CryptoKeyFeatureTailnetResume ,
944
+ StartsAt : now .Add (- time .Hour ),
945
+ Sequence : 1 ,
946
+ })
947
+ _ = dbgen .CryptoKey (t , db , database.CryptoKey {
948
+ Feature : database .CryptoKeyFeatureOidcConvert ,
949
+ StartsAt : now .Add (- time .Hour ),
950
+ Sequence : 1 ,
951
+ })
952
+
953
+ proxy := coderdenttest .NewWorkspaceProxyReplica (t , api , cclient , & coderdenttest.ProxyOptions {
954
+ Name : testutil .GetRandomName (t ),
955
+ })
956
+
957
+ keys , err := proxy .SDKClient .CryptoKeys (ctx )
958
+ require .NoError (t , err )
959
+ require .NotEmpty (t , keys )
960
+ require .Equal (t , 2 , len (keys .CryptoKeys ))
961
+ require .Contains (t , keys .CryptoKeys , key1 )
962
+ require .Contains (t , keys .CryptoKeys , key2 )
963
+ })
964
+
965
+ t .Run ("Unauthorized" , func (t * testing.T ) {
966
+ t .Parallel ()
967
+
968
+ ctx := testutil .Context (t , testutil .WaitMedium )
969
+ db , pubsub := dbtestutil .NewDB (t )
970
+ cclient , _ , api , _ := coderdenttest .NewWithAPI (t , & coderdenttest.Options {
971
+ Options : & coderdtest.Options {
972
+ Database : db ,
973
+ Pubsub : pubsub ,
974
+ IncludeProvisionerDaemon : true ,
975
+ },
976
+ LicenseOptions : & coderdenttest.LicenseOptions {
977
+ Features : license.Features {
978
+ codersdk .FeatureWorkspaceProxy : 1 ,
979
+ },
980
+ },
981
+ })
982
+
983
+ _ = coderdenttest .NewWorkspaceProxyReplica (t , api , cclient , & coderdenttest.ProxyOptions {
984
+ Name : testutil .GetRandomName (t ),
985
+ })
986
+
987
+ client := wsproxysdk .New (cclient .URL )
988
+ client .SetSessionToken (cclient .SessionToken ())
989
+
990
+ _ , err := client .CryptoKeys (ctx )
991
+ require .Error (t , err )
992
+ var sdkErr * codersdk.Error
993
+ require .ErrorAs (t , err , & sdkErr )
994
+ require .Equal (t , http .StatusUnauthorized , sdkErr .StatusCode ())
995
+ })
996
+ }
997
+
998
+ func fromDBCryptoKeys (key database.CryptoKey ) wsproxysdk.CryptoKey {
999
+ return wsproxysdk.CryptoKey {
1000
+ Feature : wsproxysdk .CryptoKeyFeature (key .Feature ),
1001
+ Sequence : key .Sequence ,
1002
+ StartsAt : key .StartsAt .UTC (),
1003
+ DeletesAt : key .DeletesAt .Time .UTC (),
1004
+ Secret : key .Secret .String ,
1005
+ }
1006
+ }
0 commit comments