github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/identityfile/identityfile_test.go (about)

     1  /*
     2  Copyright 2021 Gravitational, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  
    16  */
    17  
    18  package identityfile
    19  
    20  import (
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	"github.com/stretchr/testify/require"
    26  	"golang.org/x/crypto/ssh"
    27  )
    28  
    29  // TestIdentityFileBasics verifies basic profile operations such as
    30  // load/store and setting current.
    31  func TestIdentityFileBasics(t *testing.T) {
    32  	t.Parallel()
    33  	path := filepath.Join(t.TempDir(), "file")
    34  	writeIDFile := &IdentityFile{
    35  		PrivateKey: []byte("-----BEGIN RSA PRIVATE KEY-----\nkey\n-----END RSA PRIVATE KEY-----\n"),
    36  		Certs: Certs{
    37  			SSH: append([]byte(ssh.CertAlgoRSAv01), '\n'),
    38  			TLS: []byte("-----BEGIN CERTIFICATE-----\ntls-cert\n-----END CERTIFICATE-----\n"),
    39  		},
    40  		CACerts: CACerts{
    41  			SSH: [][]byte{[]byte("@cert-authority ssh-cacerts\n")},
    42  			TLS: [][]byte{[]byte("-----BEGIN CERTIFICATE-----\ntls-cacerts\n-----END CERTIFICATE-----\n")},
    43  		},
    44  	}
    45  
    46  	// Write identity file
    47  	err := Write(writeIDFile, path)
    48  	require.NoError(t, err)
    49  
    50  	// Read identity file from file
    51  	readIDFile, err := ReadFile(path)
    52  	require.NoError(t, err)
    53  	require.Equal(t, writeIDFile, readIDFile)
    54  
    55  	// Read identity file from string
    56  	s, err := os.ReadFile(path)
    57  	require.NoError(t, err)
    58  	fromStringIDFile, err := FromString(string(s))
    59  	require.NoError(t, err)
    60  	require.Equal(t, writeIDFile, fromStringIDFile)
    61  }
    62  
    63  func TestIsSSHCert(t *testing.T) {
    64  	for _, tc := range []struct {
    65  		certType   string
    66  		expectBool bool
    67  	}{
    68  		{
    69  			certType:   "opensesame@openssh.com",
    70  			expectBool: false,
    71  		}, {
    72  			certType:   ssh.CertAlgoRSAv01,
    73  			expectBool: true,
    74  		}, {
    75  			certType:   ssh.CertAlgoECDSA256v01,
    76  			expectBool: true,
    77  		}, {
    78  			certType:   ssh.CertAlgoED25519v01,
    79  			expectBool: true,
    80  		},
    81  	} {
    82  		t.Run(tc.certType, func(t *testing.T) {
    83  			certData := append([]byte(tc.certType), []byte(" AAAA...")...)
    84  			isSSHCert := isSSHCert(certData)
    85  			require.Equal(t, tc.expectBool, isSSHCert)
    86  		})
    87  	}
    88  }