@@ -765,6 +765,97 @@ func TestCheckVarz(t *testing.T) {
765765 })
766766}
767767
768+ func TestCheckCredential (t * testing.T ) {
769+ noExpiry := `-----BEGIN NATS USER JWT-----
770+ eyJ0eXAiOiJKV1QiLCJhbGciOiJlZDI1NTE5LW5rZXkifQ.eyJqdGkiOiJBSUdIM0I2TEFGQkMzNktaSFJCSFI1QVZaTVFHQkdDS0NRTlNXRFBMN0U1NE5SM0I1SkxRIiwiaWF0IjoxNjk1MzY5NjU1LCJpc3MiOiJBRFFCT1haQTZaWk5MMko0VFpZNTZMUVpUN1FCVk9DNDVLVlQ3UDVNWkZVWU1LSVpaTUdaSE02QSIsIm5hbWUiOiJib2IiLCJzdWIiOiJVQkhPVDczREVGN1dZWUZUS1ZVSDZNWDNFUUVZSlFWWUNBRUJXUFJaSDNYR0E2WDdLRDNGUkFYSCIsIm5hdHMiOnsicHViIjp7fSwic3ViIjp7fSwic3VicyI6LTEsImRhdGEiOi0xLCJwYXlsb2FkIjotMSwidHlwZSI6InVzZXIiLCJ2ZXJzaW9uIjoyfX0.kGsxvI3NNNp60unItd-Eo1Yw6B9T3rBOeq7lvRY_klP5yTaBZwhCTKUNYdr_n2HNkCNB44fyW2_pmBhDki_CDQ
771+ ------END NATS USER JWT------
772+
773+ ************************* IMPORTANT *************************
774+ NKEY Seed printed below can be used to sign and prove identity.
775+ NKEYs are sensitive and should be treated as secrets.
776+
777+ -----BEGIN USER NKEY SEED-----
778+ SUAIQJDZJGYOJN4NBOLYRRENCNTPXZ7PPVQW7RWEXWJUNBAFDRPDO27JWA
779+ ------END USER NKEY SEED------
780+
781+ *************************************************************`
782+
783+ expires2100 := `-----BEGIN NATS USER JWT-----
784+ eyJ0eXAiOiJKV1QiLCJhbGciOiJlZDI1NTE5LW5rZXkifQ.eyJleHAiOjQxMDI0NDQ4MDAsImp0aSI6IlhRQkNTUUo3M0c3STRWR0JVUUNNQjdKRVlDWlVNUzdLUzJPU0Q1Skk3WjY0NEE0TU40SUEiLCJpYXQiOjE2OTUzNzA4OTcsImlzcyI6IkFERU5CTlBZSUwzTklXVkxCMjJVUU5FR0NMREhGSllNSUxEVEFQSlk1SFlQV05LQVZQNzJXREFSIiwibmFtZSI6ImJvYiIsInN1YiI6IlVCTTdYREtRUzRRQVBKUEFCSllWSU5RR1lETko2R043MjZNQ01DV0VZRDJTTU9GQVZOQ1E1M09IIiwibmF0cyI6eyJwdWIiOnt9LCJzdWIiOnt9LCJzdWJzIjotMSwiZGF0YSI6LTEsInBheWxvYWQiOi0xLCJ0eXBlIjoidXNlciIsInZlcnNpb24iOjJ9fQ.3ytewtkFoRLKNeRJjPGOyNWeeQKqKdfHmyRL2ofaUiqj_OoN2LAmg_Ms2zpU-A_2xAiUH7VsMIRJxw1cx3bwAg
785+ ------END NATS USER JWT------
786+
787+ ************************* IMPORTANT *************************
788+ NKEY Seed printed below can be used to sign and prove identity.
789+ NKEYs are sensitive and should be treated as secrets.
790+
791+ -----BEGIN USER NKEY SEED-----
792+ SUAKYITMHPMSYUGPNQBLLPGOPFQN44XNCGXHNSHLJJVMD3IKYGBOLAI7TI
793+ ------END USER NKEY SEED------
794+
795+ *************************************************************`
796+
797+ writeCred := func (t * testing.T , cred string ) string {
798+ tf , err := os .CreateTemp ("" , "" )
799+ assertNoError (t , err )
800+
801+ tf .Write ([]byte (cred ))
802+ tf .Close ()
803+
804+ return tf .Name ()
805+ }
806+
807+ t .Run ("no expiry" , func (t * testing.T ) {
808+ cmd := & SrvCheckCmd {}
809+ cmd .credential = writeCred (t , noExpiry )
810+ defer func (f string ) { os .Remove (f ) }(cmd .credential )
811+
812+ cmd .credentialRequiresExpire = true
813+
814+ check := & monitor.Result {}
815+ assertNoError (t , cmd .checkCredential (check ))
816+ assertListEquals (t , check .Criticals , "never expires" )
817+ assertListIsEmpty (t , check .Warnings )
818+
819+ cmd .credential = writeCred (t , expires2100 )
820+ defer func (f string ) { os .Remove (f ) }(cmd .credential )
821+
822+ check = & monitor.Result {}
823+ assertNoError (t , cmd .checkCredential (check ))
824+ assertListIsEmpty (t , check .Criticals )
825+ assertListIsEmpty (t , check .Warnings )
826+ assertListEquals (t , check .OKs , "expires in 2100-01-01 00:00:00 +0000 UTC" )
827+ })
828+
829+ t .Run ("critical" , func (t * testing.T ) {
830+ cmd := & SrvCheckCmd {}
831+ cmd .credential = writeCred (t , expires2100 )
832+
833+ defer os .Remove (cmd .credential )
834+
835+ check := & monitor.Result {}
836+ cmd .credentialValidityCrit = 100 * 24 * 365 * time .Hour
837+
838+ assertNoError (t , cmd .checkCredential (check ))
839+ assertListEquals (t , check .Criticals , "expires sooner than 100y0d0h0m0s" )
840+ assertListIsEmpty (t , check .Warnings )
841+ assertListIsEmpty (t , check .OKs )
842+ })
843+
844+ t .Run ("warning" , func (t * testing.T ) {
845+ cmd := & SrvCheckCmd {}
846+ cmd .credential = writeCred (t , expires2100 )
847+ defer os .Remove (cmd .credential )
848+
849+ check := & monitor.Result {}
850+ cmd .credentialValidityWarn = 100 * 24 * 365 * time .Hour
851+
852+ assertNoError (t , cmd .checkCredential (check ))
853+ assertListEquals (t , check .Warnings , "expires sooner than 100y0d0h0m0s" )
854+ assertListIsEmpty (t , check .Criticals )
855+ assertListIsEmpty (t , check .OKs )
856+ })
857+
858+ }
768859func TestCheckJSZ (t * testing.T ) {
769860 cmd := & SrvCheckCmd {}
770861
0 commit comments