github.com/zntrio/harp/v2@v2.0.9/pkg/container/identity/key/json_test.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package key 19 20 import ( 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 ) 25 26 var ( 27 legacyPrivateKey = &JSONWebKey{ 28 Kty: "OKP", 29 Crv: "X25519", 30 X: "ZxTKWxgrG341_FxatkkfAxedMtfz1zJzAm6FUmitxHM", 31 D: "ZGV0ZXJtaW5pc3RpYy1yYW5kb20tc291cmNlLWZvci0", 32 } 33 v1PrivateKey = &JSONWebKey{ 34 Kty: "OKP", 35 Crv: "Ed25519", 36 X: "2BdsL_FTiaLRwyYwlA2urcZ8TLDdisbzBSEp-LUuHos", 37 D: "ZGV0ZXJtaW5pc3RpYy1yYW5kb20tc291cmNlLWZvci3YF2wv8VOJotHDJjCUDa6txnxMsN2KxvMFISn4tS4eiw", 38 } 39 v2PrivateKey = &JSONWebKey{ 40 Kty: "EC", 41 Crv: "P-384", 42 X: "RfbSuUTw-qn5igwbxI06in3XwDJ-hIX9H1nswXm8_mdShz9lJFZq5BHpwvgOqCtE", 43 Y: "ag16lWruEPkhWChmZnO52ne1iyLGAEVNbyx38NPMOqNZzV7yP9ugrzCa7pCz8eBr", 44 D: "aXN0aWMtcmFuZG9tLXNvdYiXCnZ-xg0Te8QN3AId4n-bdBdDfhXJjz1OngEo78g8", 45 } 46 ) 47 48 func TestJSONWebKey_RecoveryKey(t *testing.T) { 49 t.Run("D has invalid encoding", func(t *testing.T) { 50 id, err := (&JSONWebKey{ 51 D: "é", 52 }).RecoveryKey() 53 assert.Error(t, err) 54 assert.Empty(t, id) 55 }) 56 57 t.Run("unhandled private key", func(t *testing.T) { 58 id, err := (&JSONWebKey{ 59 Crv: "P-256", 60 }).RecoveryKey() 61 assert.Error(t, err) 62 assert.Empty(t, id) 63 }) 64 65 t.Run("valid - legacy", func(t *testing.T) { 66 id, err := legacyPrivateKey.RecoveryKey() 67 assert.NoError(t, err) 68 assert.Equal(t, "ZGV0ZXJtaW5pc3RpYy1yYW5kb20tc291cmNlLWZvci0", id) 69 }) 70 71 t.Run("valid - v1", func(t *testing.T) { 72 id, err := v1PrivateKey.RecoveryKey() 73 assert.NoError(t, err) 74 assert.Equal(t, "v1.ck.6Of3g6qt-NPBzXSMNl4jPIZbrZIIwonT2pn7GCc4i3o", id) 75 }) 76 77 t.Run("valid - v2", func(t *testing.T) { 78 id, err := v2PrivateKey.RecoveryKey() 79 assert.NoError(t, err) 80 assert.Equal(t, "v2.ck.aXN0aWMtcmFuZG9tLXNvdYiXCnZ-xg0Te8QN3AId4n-bdBdDfhXJjz1OngEo78g8", id) 81 }) 82 }