11package main
22
33import (
4+ "encoding/base64"
5+ "encoding/json"
46 "fmt"
57 "os"
68 "testing"
79
10+ "github.com/stretchr/testify/assert"
811 "github.com/stretchr/testify/require"
912)
1013
@@ -30,3 +33,82 @@ func TestDaemonRequestsLinux(t *testing.T) {
3033 os .Remove ("test.crt" )
3134 os .Remove ("test.key" )
3235}
36+
37+ func TestErrorBetweenSavingAndSigning (t * testing.T ) {
38+ _ , baseURL , _ , cleanUp := daemonInit (t , "" )
39+ defer os .Remove ("test.crt" )
40+ defer os .Remove ("test.key" )
41+ defer os .Remove ("test.csr" )
42+
43+ postData , err := json .Marshal (map [string ]any {
44+ "Country" : "DE" ,
45+ "State" : "Bavaria" ,
46+ "Locality" : "Earth" ,
47+ "Organization" : "snclient" ,
48+ "OrganizationalUnit" : "IT" ,
49+ "HostName" : "Root CA SNClient" ,
50+ "NewKey" : true ,
51+ "KeyLength" : 1024 ,
52+ })
53+ require .NoErrorf (t , err , "post data json encoded" )
54+
55+ // Create Temp Server Certs
56+ runCmd (t , & cmd {
57+ Cmd : "make" ,
58+ Args : []string {"testca" },
59+ ErrLike : []string {"Certificate request self-signature ok" },
60+ })
61+ defer runCmd (t , & cmd {
62+ Cmd : "make" ,
63+ Args : []string {"clean-testca" },
64+ Like : []string {"dist" },
65+ })
66+
67+ commandResult := runCmd (t , & cmd {
68+ Cmd : "curl" ,
69+ Args : []string {"-s" , "-u" , "user:" + localDaemonAdminPassword , "-k" , "-s" , "-d" , string (postData ), baseURL + "/api/v1/admin/csr" },
70+ Dir : "." ,
71+ Like : []string {"CERTIFICATE REQUEST" },
72+ })
73+ err = os .WriteFile ("test.csr" , []byte (commandResult .Stdout ), 0o600 )
74+ if err != nil {
75+ t .Fatalf ("could not save certificate signing requests" )
76+ }
77+
78+ runCmd (t , & cmd {
79+ Cmd : "openssl" ,
80+ Args : []string {"x509" , "-req" , "-in=test.csr" , "-CA=dist/cacert.pem" , "-CAkey=dist/ca.key" , "-out=server.crt" , "-days=365" },
81+ ErrLike : []string {"Certificate request self-signature ok" },
82+ })
83+ defer os .Remove ("server.crt" )
84+
85+ keyBak , _ := os .ReadFile ("test.key.tmp" )
86+ newCert , _ := os .ReadFile ("server.crt" )
87+
88+ // restart client
89+ cleanUp ()
90+ _ , baseURL , _ , cleanUp = daemonInit (t , "" )
91+ defer cleanUp ()
92+
93+ postData , err = json .Marshal (map [string ]interface {}{
94+ "Reload" : true ,
95+ "CertData" : base64 .StdEncoding .EncodeToString (newCert ),
96+ "KeyData" : "" ,
97+ })
98+ require .NoErrorf (t , err , "post data json encoded" )
99+
100+ runCmd (t , & cmd {
101+ Cmd : "curl" ,
102+ Args : []string {"-s" , "-u" , "user:" + localDaemonAdminPassword , "-k" , "-s" , "-d" , string (postData ), baseURL + "/api/v1/admin/certs/replace" },
103+ Like : []string {`{"success":true}` },
104+ })
105+
106+ // Check if new private Key matches the on we got from the csr Endpoint
107+ key , _ := os .ReadFile ("test.key" )
108+ assert .Equalf (t , string (keyBak ), string (key ), "private keys do not match" )
109+
110+ _ , err = os .ReadFile ("test.key.tmp" )
111+ if err == nil {
112+ t .Fatalf ("tempory key file was not removed" )
113+ }
114+ }
0 commit comments