@@ -745,6 +745,26 @@ def expect_lockfile_to_contain(pid)
745745 state . next_state
746746 } . to raise_error ( OpenSSL ::PKey ::RSAError )
747747 end
748+
749+ it "transitions to Done if current time plus renewal interval is less than cert's \" NotAfter\" time" do
750+ allow ( cert_provider ) . to receive ( :load_private_key ) . and_return ( private_key )
751+ allow ( cert_provider ) . to receive ( :load_client_cert ) . and_return ( client_cert )
752+
753+ st = state . next_state
754+ expect ( st ) . to be_instance_of ( Puppet ::SSL ::StateMachine ::Done )
755+ end
756+
757+ it "returns NeedRenewedCert if current time plus renewal interval is greater than cert's \" NotAfter\" time" do
758+ client_cert . not_after = ( Time . now + 300 )
759+ allow ( cert_provider ) . to receive ( :load_private_key ) . and_return ( private_key )
760+ allow ( cert_provider ) . to receive ( :load_client_cert ) . and_return ( client_cert )
761+
762+ ssl_context = Puppet ::SSL ::SSLContext . new ( cacerts : [ cacert ] , client_cert : client_cert , crls : [ crl ] )
763+ state = Puppet ::SSL ::StateMachine ::NeedKey . new ( machine , ssl_context )
764+
765+ st = state . next_state
766+ expect ( st ) . to be_instance_of ( Puppet ::SSL ::StateMachine ::NeedRenewedCert )
767+ end
748768 end
749769
750770 context 'in state NeedSubmitCSR' do
@@ -1049,5 +1069,48 @@ def write_csr_attributes(data)
10491069 expect ( state . next_state ) . to be_an_instance_of ( Puppet ::SSL ::StateMachine ::LockFailure )
10501070 end
10511071 end
1072+
1073+ context 'in state NeedRenewedCert' do
1074+ before :each do
1075+ client_cert . not_after = ( Time . now + 300 )
1076+ end
1077+
1078+ let ( :ssl_context ) { Puppet ::SSL ::SSLContext . new ( cacerts : cacerts , client_cert : client_cert , crls : crls , ) }
1079+ let ( :state ) { Puppet ::SSL ::StateMachine ::NeedRenewedCert . new ( machine , ssl_context , private_key ) }
1080+ let ( :renewed_cert ) { cert_fixture ( 'renewed.pem' ) }
1081+
1082+ it 'returns Done with renewed cert when successful' do
1083+ allow ( cert_provider ) . to receive ( :save_client_cert )
1084+ stub_request ( :post , %r{puppet-ca/v1/certificate_renewal} ) . to_return ( status : 200 , body : renewed_cert . to_pem )
1085+
1086+ st = state . next_state
1087+ expect ( st ) . to be_an_instance_of ( Puppet ::SSL ::StateMachine ::Done )
1088+ expect ( st . ssl_context [ :client_cert ] ) . to eq ( renewed_cert )
1089+ end
1090+
1091+ it 'logs a warning message when failing with a non-404 status' do
1092+ stub_request ( :post , %r{puppet-ca/v1/certificate_renewal} ) . to_return ( status : 400 , body : 'Failed to automatically renew certificate: 400 Bad request' )
1093+
1094+ expect ( Puppet ) . to receive ( :warning ) . with ( /Failed to automatically renew certificate/ )
1095+ st = state . next_state
1096+ expect ( st ) . to be_an_instance_of ( Puppet ::SSL ::StateMachine ::Done )
1097+ end
1098+
1099+ it 'logs an info message when failing with 404' do
1100+ stub_request ( :post , %r{puppet-ca/v1/certificate_renewal} ) . to_return ( status : 404 , body : 'Certificate autorenewal has not been enabled on the server.' )
1101+
1102+ expect ( Puppet ) . to receive ( :info ) . with ( 'Certificate autorenewal has not been enabled on the server.' )
1103+ st = state . next_state
1104+ expect ( st ) . to be_an_instance_of ( Puppet ::SSL ::StateMachine ::Done )
1105+ end
1106+
1107+ it 'logs a warning message when failing with no HTTP status' do
1108+ stub_request ( :post , %r{puppet-ca/v1/certificate_renewal} ) . to_raise ( Errno ::ECONNREFUSED )
1109+
1110+ expect ( Puppet ) . to receive ( :warning ) . with ( /Unable to automatically renew certificate:/ )
1111+ st = state . next_state
1112+ expect ( st ) . to be_an_instance_of ( Puppet ::SSL ::StateMachine ::Done )
1113+ end
1114+ end
10521115 end
10531116end
0 commit comments