github.com/slackhq/nebula@v1.9.0/cmd/nebula-cert/print_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/slackhq/nebula/cert"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func Test_printSummary(t *testing.T) {
    14  	assert.Equal(t, "print <flags>: prints details about a certificate", printSummary())
    15  }
    16  
    17  func Test_printHelp(t *testing.T) {
    18  	ob := &bytes.Buffer{}
    19  	printHelp(ob)
    20  	assert.Equal(
    21  		t,
    22  		"Usage of "+os.Args[0]+" print <flags>: prints details about a certificate\n"+
    23  			"  -json\n"+
    24  			"    \tOptional: outputs certificates in json format\n"+
    25  			"  -out-qr string\n"+
    26  			"    \tOptional: output a qr code image (png) of the certificate\n"+
    27  			"  -path string\n"+
    28  			"    \tRequired: path to the certificate\n",
    29  		ob.String(),
    30  	)
    31  }
    32  
    33  func Test_printCert(t *testing.T) {
    34  	// Orient our local time and avoid headaches
    35  	time.Local = time.UTC
    36  	ob := &bytes.Buffer{}
    37  	eb := &bytes.Buffer{}
    38  
    39  	// no path
    40  	err := printCert([]string{}, ob, eb)
    41  	assert.Equal(t, "", ob.String())
    42  	assert.Equal(t, "", eb.String())
    43  	assertHelpError(t, err, "-path is required")
    44  
    45  	// no cert at path
    46  	ob.Reset()
    47  	eb.Reset()
    48  	err = printCert([]string{"-path", "does_not_exist"}, ob, eb)
    49  	assert.Equal(t, "", ob.String())
    50  	assert.Equal(t, "", eb.String())
    51  	assert.EqualError(t, err, "unable to read cert; open does_not_exist: "+NoSuchFileError)
    52  
    53  	// invalid cert at path
    54  	ob.Reset()
    55  	eb.Reset()
    56  	tf, err := os.CreateTemp("", "print-cert")
    57  	assert.Nil(t, err)
    58  	defer os.Remove(tf.Name())
    59  
    60  	tf.WriteString("-----BEGIN NOPE-----")
    61  	err = printCert([]string{"-path", tf.Name()}, ob, eb)
    62  	assert.Equal(t, "", ob.String())
    63  	assert.Equal(t, "", eb.String())
    64  	assert.EqualError(t, err, "error while unmarshaling cert: input did not contain a valid PEM encoded block")
    65  
    66  	// test multiple certs
    67  	ob.Reset()
    68  	eb.Reset()
    69  	tf.Truncate(0)
    70  	tf.Seek(0, 0)
    71  	c := cert.NebulaCertificate{
    72  		Details: cert.NebulaCertificateDetails{
    73  			Name:      "test",
    74  			Groups:    []string{"hi"},
    75  			PublicKey: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2},
    76  		},
    77  		Signature: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2},
    78  	}
    79  
    80  	p, _ := c.MarshalToPEM()
    81  	tf.Write(p)
    82  	tf.Write(p)
    83  	tf.Write(p)
    84  
    85  	err = printCert([]string{"-path", tf.Name()}, ob, eb)
    86  	assert.Nil(t, err)
    87  	assert.Equal(
    88  		t,
    89  		"NebulaCertificate {\n\tDetails {\n\t\tName: test\n\t\tIps: []\n\t\tSubnets: []\n\t\tGroups: [\n\t\t\t\"hi\"\n\t\t]\n\t\tNot before: 0001-01-01 00:00:00 +0000 UTC\n\t\tNot After: 0001-01-01 00:00:00 +0000 UTC\n\t\tIs CA: false\n\t\tIssuer: \n\t\tPublic key: 0102030405060708090001020304050607080900010203040506070809000102\n\t\tCurve: CURVE25519\n\t}\n\tFingerprint: cc3492c0e9c48f17547f5987ea807462ebb3451e622590a10bb3763c344c82bd\n\tSignature: 0102030405060708090001020304050607080900010203040506070809000102\n}\nNebulaCertificate {\n\tDetails {\n\t\tName: test\n\t\tIps: []\n\t\tSubnets: []\n\t\tGroups: [\n\t\t\t\"hi\"\n\t\t]\n\t\tNot before: 0001-01-01 00:00:00 +0000 UTC\n\t\tNot After: 0001-01-01 00:00:00 +0000 UTC\n\t\tIs CA: false\n\t\tIssuer: \n\t\tPublic key: 0102030405060708090001020304050607080900010203040506070809000102\n\t\tCurve: CURVE25519\n\t}\n\tFingerprint: cc3492c0e9c48f17547f5987ea807462ebb3451e622590a10bb3763c344c82bd\n\tSignature: 0102030405060708090001020304050607080900010203040506070809000102\n}\nNebulaCertificate {\n\tDetails {\n\t\tName: test\n\t\tIps: []\n\t\tSubnets: []\n\t\tGroups: [\n\t\t\t\"hi\"\n\t\t]\n\t\tNot before: 0001-01-01 00:00:00 +0000 UTC\n\t\tNot After: 0001-01-01 00:00:00 +0000 UTC\n\t\tIs CA: false\n\t\tIssuer: \n\t\tPublic key: 0102030405060708090001020304050607080900010203040506070809000102\n\t\tCurve: CURVE25519\n\t}\n\tFingerprint: cc3492c0e9c48f17547f5987ea807462ebb3451e622590a10bb3763c344c82bd\n\tSignature: 0102030405060708090001020304050607080900010203040506070809000102\n}\n",
    90  		ob.String(),
    91  	)
    92  	assert.Equal(t, "", eb.String())
    93  
    94  	// test json
    95  	ob.Reset()
    96  	eb.Reset()
    97  	tf.Truncate(0)
    98  	tf.Seek(0, 0)
    99  	c = cert.NebulaCertificate{
   100  		Details: cert.NebulaCertificateDetails{
   101  			Name:      "test",
   102  			Groups:    []string{"hi"},
   103  			PublicKey: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2},
   104  		},
   105  		Signature: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2},
   106  	}
   107  
   108  	p, _ = c.MarshalToPEM()
   109  	tf.Write(p)
   110  	tf.Write(p)
   111  	tf.Write(p)
   112  
   113  	err = printCert([]string{"-json", "-path", tf.Name()}, ob, eb)
   114  	assert.Nil(t, err)
   115  	assert.Equal(
   116  		t,
   117  		"{\"details\":{\"curve\":\"CURVE25519\",\"groups\":[\"hi\"],\"ips\":[],\"isCa\":false,\"issuer\":\"\",\"name\":\"test\",\"notAfter\":\"0001-01-01T00:00:00Z\",\"notBefore\":\"0001-01-01T00:00:00Z\",\"publicKey\":\"0102030405060708090001020304050607080900010203040506070809000102\",\"subnets\":[]},\"fingerprint\":\"cc3492c0e9c48f17547f5987ea807462ebb3451e622590a10bb3763c344c82bd\",\"signature\":\"0102030405060708090001020304050607080900010203040506070809000102\"}\n{\"details\":{\"curve\":\"CURVE25519\",\"groups\":[\"hi\"],\"ips\":[],\"isCa\":false,\"issuer\":\"\",\"name\":\"test\",\"notAfter\":\"0001-01-01T00:00:00Z\",\"notBefore\":\"0001-01-01T00:00:00Z\",\"publicKey\":\"0102030405060708090001020304050607080900010203040506070809000102\",\"subnets\":[]},\"fingerprint\":\"cc3492c0e9c48f17547f5987ea807462ebb3451e622590a10bb3763c344c82bd\",\"signature\":\"0102030405060708090001020304050607080900010203040506070809000102\"}\n{\"details\":{\"curve\":\"CURVE25519\",\"groups\":[\"hi\"],\"ips\":[],\"isCa\":false,\"issuer\":\"\",\"name\":\"test\",\"notAfter\":\"0001-01-01T00:00:00Z\",\"notBefore\":\"0001-01-01T00:00:00Z\",\"publicKey\":\"0102030405060708090001020304050607080900010203040506070809000102\",\"subnets\":[]},\"fingerprint\":\"cc3492c0e9c48f17547f5987ea807462ebb3451e622590a10bb3763c344c82bd\",\"signature\":\"0102030405060708090001020304050607080900010203040506070809000102\"}\n",
   118  		ob.String(),
   119  	)
   120  	assert.Equal(t, "", eb.String())
   121  }