How to use Certificate method of lib Package

Best K6 code snippet using lib.Certificate

certificate.go

Source:certificate.go Github

copy

Full Screen

...22 "github.com/bfenetworks/api-server/model/itxn"23 "github.com/bfenetworks/api-server/model/iversion_control"24 "github.com/bfenetworks/bfe/bfe_tls"25)26type Certificate struct {27 CertName string28 Description string29 IsDefault bool30 CertFileName string31 CertFilePath string32 KeyFileName string33 KeyFilePath string34 ExpiredDate string35 Products []*ibasic.Product36}37type CertificateFilter struct {38 CertName *string39 IsDefault *bool40}41type CertificateParam struct {42 CertName *string43 Description *string44 IsDefault *bool45 CertFileName *string46 CertFilePath *string47 CertFileContent *string48 KeyFileName *string49 KeyFileContent *string50 KeyFilePath *string51 ExpiredDate *string52}53type CertificateStorager interface {54 FetchCertificates(context.Context, *CertificateFilter) ([]*Certificate, error)55 DeleteCertificate(context.Context, *Certificate) error56 CreateCertificate(context.Context, *CertificateParam) error57 UpdateCertificate(context.Context, *Certificate, *CertificateParam) error58}59type CertificateManager struct {60 storager CertificateStorager61 txn itxn.TxnStorager62 extraFileStorager ibasic.ExtraFileStorager63 versionControlManager *iversion_control.VersionControlManager64}65func NewCertificateManager(txn itxn.TxnStorager, storager CertificateStorager,66 versionControlManager *iversion_control.VersionControlManager,67 extraFileStorager ibasic.ExtraFileStorager) *CertificateManager {68 return &CertificateManager{69 txn: txn,70 storager: storager,71 extraFileStorager: extraFileStorager,72 versionControlManager: versionControlManager,73 }74}75func (pm *CertificateManager) FetchCertificates(ctx context.Context, param *CertificateFilter) (list []*Certificate, err error) {76 err = pm.txn.AtomExecute(ctx, func(ctx context.Context) error {77 list, err = pm.storager.FetchCertificates(ctx, param)78 return err79 })80 return81}82func (pm *CertificateManager) DeleteCertificate(ctx context.Context, certificate *Certificate) (err error) {83 if certificate.IsDefault {84 return xerror.WrapModelErrorWithMsg("Cant Delete Default Certificate")85 }86 if len(certificate.Products) > 0 {87 return xerror.WrapModelErrorWithMsg("Cant Delete Certificate Be Refer By Product")88 }89 return pm.txn.AtomExecute(ctx, func(ctx context.Context) error {90 if err := pm.extraFileStorager.DeleteExtraFile(ctx, &ibasic.ExtraFileFilter{91 Names: []string{certificate.CertFilePath, certificate.KeyFilePath},92 }); err != nil {93 return err94 }95 return pm.storager.DeleteCertificate(ctx, certificate)96 })97}98func validateCertPair(certFileName string, certFileContent string, keyFileName string, keyFileContent string) error {99 checkCertFileInfo := func(certPEMBlock []byte) error {100 var certDERBlock *pem.Block101 for {102 certDERBlock, certPEMBlock = pem.Decode(certPEMBlock)103 if certDERBlock == nil {104 break105 }106 if certDERBlock.Type != "CERTIFICATE" {107 return xerror.WrapParamErrorWithMsg("Certificate File Format Must Be PEM")108 }109 }110 return nil111 }112 if err := checkCertFileInfo([]byte(certFileContent)); err != nil {113 return err114 }115 checkKeyFileInfo := func(keyPEMBlock []byte) error {116 var keyDERBlock *pem.Block117 keyDERBlock, _ = pem.Decode(keyPEMBlock)118 if keyDERBlock == nil {119 return xerror.WrapParamErrorWithMsg("Certificate Private Key File Format Must Be PEM")120 }121 if keyDERBlock.Type != "PRIVATE KEY" && !strings.HasSuffix(keyDERBlock.Type, " PRIVATE KEY") {122 return xerror.WrapParamErrorWithMsg("Certificate Private Key File Format Must Be PEM")123 }124 return nil125 }126 if err := checkKeyFileInfo([]byte(keyFileContent)); err != nil {127 return err128 }129 checkCertKeyPair := func(certPEMBlock, keyPEMBlock []byte) error {130 _, err := bfe_tls.X509KeyPair(certPEMBlock, keyPEMBlock)131 return err132 }133 if err := checkCertKeyPair([]byte(certFileContent), []byte(keyFileContent)); err != nil {134 return err135 }136 return nil137}138func (pm *CertificateManager) CreateCertificate(ctx context.Context, param *CertificateParam) (err error) {139 if err = validateCertPair(*param.CertFileName, *param.CertFileContent, *param.KeyFileName, *param.KeyFileContent); err != nil {140 return err141 }142 names := []string{143 *param.CertFileName,144 *param.KeyFileName,145 }146 existedNames := map[string]bool{}147 for _, one := range names {148 if existedNames[one] {149 return xerror.WrapParamErrorWithMsg("Certificate File Name %s Existed", one)150 }151 existedNames[one] = true152 }153 err = pm.txn.AtomExecute(ctx, func(ctx context.Context) error {154 list, err := pm.storager.FetchCertificates(ctx, nil)155 if err != nil {156 return err157 }158 var defaultCertificate *Certificate159 // validate name unique160 for _, one := range list {161 if one.CertName == *param.CertName {162 return xerror.WrapRecordExisted("Certification")163 }164 for _, name := range names {165 if one.CertFileName == name || one.KeyFileName == name {166 return xerror.WrapModelErrorWithMsg("Certificate File Name %s By Used By %s", name, one.CertName)167 }168 }169 if one.IsDefault {170 defaultCertificate = one171 }172 }173 if defaultCertificate == nil && (param.IsDefault == nil || !*param.IsDefault) {174 return xerror.WrapModelErrorWithMsg("Must Has Default Certificate")175 }176 // check/update default177 if isDefault := param.IsDefault; isDefault != nil && *isDefault && defaultCertificate != nil {178 if err = pm.storager.UpdateCertificate(ctx, defaultCertificate, &CertificateParam{179 IsDefault: lib.PBool(false),180 }); err != nil {181 return err182 }183 }184 param.CertFilePath = lib.PString(ibasic.ExtraFilePath(tlsConfDir, ibasic.BuildinProduct, *param.CertFileName))185 param.KeyFilePath = lib.PString(ibasic.ExtraFilePath(tlsConfDir, ibasic.BuildinProduct, *param.KeyFileName))186 if err := pm.extraFileStorager.CreateExtraFile(ctx, ibasic.BuildinProduct, &ibasic.ExtraFileParam{187 Name: param.CertFilePath,188 Content: []byte(*param.CertFileContent),189 }, &ibasic.ExtraFileParam{190 Name: param.KeyFilePath,191 Content: []byte(*param.KeyFileContent),192 }); err != nil {193 return err194 }195 return pm.storager.CreateCertificate(ctx, param)196 })197 return198}199func (pm *CertificateManager) UpdateAsDefaultCertificate(ctx context.Context, cert *Certificate) (err error) {200 if cert.IsDefault {201 return nil202 }203 return pm.txn.AtomExecute(ctx, func(ctx context.Context) error {204 list, err := pm.storager.FetchCertificates(ctx, &CertificateFilter{205 IsDefault: lib.PBool(true),206 })207 if err != nil {208 return err209 }210 if len(list) != 0 {211 if err = pm.storager.UpdateCertificate(ctx, list[0], &CertificateParam{212 IsDefault: lib.PBool(false),213 }); err != nil {214 return err215 }216 }217 return pm.storager.UpdateCertificate(ctx, cert, &CertificateParam{218 IsDefault: lib.PBool(true),219 })220 })221}...

Full Screen

Full Screen

certificate_test.go

Source:certificate_test.go Github

copy

Full Screen

...14 "github.com/hyperledger/fabric-ca/util"15 "github.com/pkg/errors"16)17// Unit Tests18func TestNewCertificateCommand(t *testing.T) {19 cmd := new(mocks.Command)20 cmd.On("GetViper").Return(viper.New())21 certCmd := newCertificateCommand(cmd)22 assert.NotNil(t, certCmd)23}24func TestAddCertificateCommand(t *testing.T) {25 cmd := new(mocks.Command)26 cmd.On("GetViper").Return(viper.New())27 certCmd := newCertificateCommand(cmd)28 assert.NotNil(t, certCmd)29 addCmd := addCertificateCommand(certCmd)30 assert.NotNil(t, addCmd)31}32func TestCreateCertificateCommand(t *testing.T) {33 cmd := new(mocks.Command)34 cmd.On("GetViper").Return(viper.New())35 certCmd := createCertificateCommand(cmd)36 assert.NotNil(t, certCmd)37}38func TestBadPreRunCertificate(t *testing.T) {39 mockBadClientCmd := new(mocks.Command)40 mockBadClientCmd.On("ConfigInit").Return(errors.New("Failed to initialize config"))41 cmd := newCertificateCommand(mockBadClientCmd)42 err := cmd.preRunCertificate(&cobra.Command{}, []string{})43 util.ErrorContains(t, err, "Failed to initialize config", "Should have failed")44}45func TestGoodPreRunCertificate(t *testing.T) {46 mockGoodClientCmd := new(mocks.Command)47 mockGoodClientCmd.On("ConfigInit").Return(nil)48 mockGoodClientCmd.On("GetClientCfg").Return(&lib.ClientConfig{})49 cmd := newCertificateCommand(mockGoodClientCmd)50 err := cmd.preRunCertificate(&cobra.Command{}, []string{})51 assert.NoError(t, err, "Should not have failed")52}53func TestFailLoadIdentity(t *testing.T) {54 mockBadClientCmd := new(mocks.Command)55 mockBadClientCmd.On("LoadMyIdentity").Return(nil, errors.New("Failed to load identity"))56 cmd := newCertificateCommand(mockBadClientCmd)57 err := cmd.runListCertificate(&cobra.Command{}, []string{})58 util.ErrorContains(t, err, "Failed to load identity", "Should have failed")59}60func TestBadRunListCertificate(t *testing.T) {61 cmd := new(mocks.Command)62 cmd.On("LoadMyIdentity").Return(&lib.Identity{}, nil)63 certCmd := newCertificateCommand(cmd)64 certCmd.timeArgs = timeArgs{65 Expiration: "30d:15d",66 }67 err := certCmd.runListCertificate(&cobra.Command{}, []string{})68 util.ErrorContains(t, err, "Invalid expiration format, expecting", "Should have failed")69}70func TestBadExpirationTime(t *testing.T) {71 cmd := new(mocks.Command)72 cmd.On("LoadMyIdentity").Return(&lib.Identity{}, nil)73 certCmd := newCertificateCommand(cmd)74 certCmd.timeArgs = timeArgs{75 Expiration: "30d:15d",76 }77 err := certCmd.getCertListReq()78 util.ErrorContains(t, err, "Invalid expiration format, expecting", "Should have failed")79 certCmd.timeArgs = timeArgs{80 Expiration: "01/30/2015::15d",81 }82 err = certCmd.getCertListReq()83 util.ErrorContains(t, err, "Invalid expiration format, use '-' instead of '/'", "Should have failed")84}85func TestGoodExpirationTime(t *testing.T) {86 cmd := new(mocks.Command)87 cmd.On("LoadMyIdentity").Return(&lib.Identity{}, nil)88 certCmd := newCertificateCommand(cmd)89 certCmd.timeArgs = timeArgs{90 Expiration: "30d::15d",91 }92 err := certCmd.getCertListReq()93 assert.NoError(t, err, "Failed to parse properly formated expiration time range")94}95func TestBadRevocationTime(t *testing.T) {96 cmd := new(mocks.Command)97 cmd.On("LoadMyIdentity").Return(&lib.Identity{}, nil)98 certCmd := newCertificateCommand(cmd)99 certCmd.timeArgs = timeArgs{100 Revocation: "30d:15d",101 }102 err := certCmd.getCertListReq()103 util.ErrorContains(t, err, "Invalid revocation format, expecting", "Should have failed")104 certCmd.timeArgs = timeArgs{105 Revocation: "1/30/2015::15d",106 }107 err = certCmd.getCertListReq()108 util.ErrorContains(t, err, "Invalid revocation format, use '-' instead of '/'", "Should have failed")109}110func TestGoodRevocationTime(t *testing.T) {111 cmd := new(mocks.Command)112 cmd.On("LoadMyIdentity").Return(&lib.Identity{}, nil)113 certCmd := newCertificateCommand(cmd)114 certCmd.timeArgs = timeArgs{115 Revocation: "30d::15d",116 }117 err := certCmd.getCertListReq()118 assert.NoError(t, err, "Failed to parse properly formated revocation time range")119}120func TestTimeRangeWithNow(t *testing.T) {121 timeNow := time.Now().UTC().Format(time.RFC3339)122 timeStr := getTime("now")123 assert.Equal(t, timeNow, timeStr)124}...

Full Screen

Full Screen

cert.go

Source:cert.go Github

copy

Full Screen

...7 pointer "github.com/mattn/go-pointer"8)9//export CertSubject10func CertSubject(ptr unsafe.Pointer) *C.char {11 cert := pointer.Restore(ptr).(*libICP.Certificate)12 return C.CString(cert.Subject)13}14//export CertSubjectMap15func CertSubjectMap(cert_ptr unsafe.Pointer) *C.icp_kvp {16 cert := pointer.Restore(cert_ptr).(*libICP.Certificate)17 output := C.new_icp_kvps(C.int(len(cert.SubjectMap)))18 i := 019 for key, val := range cert.SubjectMap {20 C.set_icp_kvp(output, C.int(i), C.CString(key), C.CString(val))21 i++22 }23 return output24}25//export CertIssuerMap26func CertIssuerMap(cert_ptr unsafe.Pointer) *C.icp_kvp {27 cert := pointer.Restore(cert_ptr).(*libICP.Certificate)28 output := C.new_icp_kvps(C.int(len(cert.IssuerMap)))29 i := 030 for key, val := range cert.IssuerMap {31 C.set_icp_kvp(output, C.int(i), C.CString(key), C.CString(val))32 i++33 }34 return output35}36//export CertFingerPrintHuman37func CertFingerPrintHuman(ptr unsafe.Pointer) *C.char {38 cert := pointer.Restore(ptr).(*libICP.Certificate)39 return C.CString(cert.FingerPrintHuman)40}41//export CertFingerPrintAlg42func CertFingerPrintAlg(ptr unsafe.Pointer) *C.char {43 cert := pointer.Restore(ptr).(*libICP.Certificate)44 return C.CString(cert.FingerPrintAlg)45}46//export CertFingerPrint47func CertFingerPrint(ptr unsafe.Pointer, n *C.int) unsafe.Pointer {48 cert := pointer.Restore(ptr).(*libICP.Certificate)49 *n = C.int(len(cert.FingerPrint))50 return C.CBytes(cert.FingerPrint)51}52//export CertIssuer53func CertIssuer(ptr unsafe.Pointer) *C.char {54 cert := pointer.Restore(ptr).(*libICP.Certificate)55 return C.CString(cert.Issuer)56}57//export CertNotBefore58func CertNotBefore(ptr unsafe.Pointer) int64 {59 cert := pointer.Restore(ptr).(*libICP.Certificate)60 return cert.NotBefore.Unix()61}62//export CertNotAfter63func CertNotAfter(ptr unsafe.Pointer) int64 {64 cert := pointer.Restore(ptr).(*libICP.Certificate)65 return cert.NotAfter.Unix()66}67//export CertIsCA68func CertIsCA(ptr unsafe.Pointer) bool {69 cert := pointer.Restore(ptr).(*libICP.Certificate)70 return cert.IsCA()71}72//export CertIsSelfSigned73func CertIsSelfSigned(ptr unsafe.Pointer) bool {74 cert := pointer.Restore(ptr).(*libICP.Certificate)75 return cert.IsSelfSigned()76}77func FinishNewCertificate(ans_certs []*libICP.Certificate, ans_errs []libICP.CodedError, certs_ptr **unsafe.Pointer, errcs_ptr **unsafe.Pointer) C.int {78 *certs_ptr = C.new_voids_ptr(C.int(len(ans_certs)))79 *errcs_ptr = C.new_voids_ptr(C.int(len(ans_errs)))80 for i := range ans_certs {81 ptr := pointer.Save(ans_certs[i])82 C.set_voids_ptr(*certs_ptr, C.int(i), ptr)83 }84 for i := range ans_errs {85 ptr := pointer.Save(ans_errs[i])86 C.set_voids_ptr(*errcs_ptr, C.int(i), ptr)87 }88 return C.int(len(ans_errs))89}90//export NewCertificateFromFile91func NewCertificateFromFile(path_c *C.char, certs_ptr **unsafe.Pointer, errcs_ptr **unsafe.Pointer) C.int {92 path := C.GoString(path_c)93 ans_certs, ans_errs := libICP.NewCertificateFromFile(path)94 return FinishNewCertificate(ans_certs, ans_errs, certs_ptr, errcs_ptr)95}96//export NewCertificateFromBytes97func NewCertificateFromBytes(data_c unsafe.Pointer, n C.int, certs_ptr **unsafe.Pointer, errcs_ptr **unsafe.Pointer) C.int {98 data := C.GoBytes(data_c, n)99 ans_certs, ans_errs := libICP.NewCertificateFromBytes(data)100 return FinishNewCertificate(ans_certs, ans_errs, certs_ptr, errcs_ptr)101}...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run K6 automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful