Skip to content

Commit

Permalink
feat: remove deprecated funcs (#14)
Browse files Browse the repository at this point in the history
* remove deprecated funcs

* cert temlate utils

* fix lint errors

* fix lint errors

* fix lint errors
  • Loading branch information
shipengqi committed Sep 23, 2022
1 parent 806d5ee commit 0faa31d
Show file tree
Hide file tree
Showing 13 changed files with 735 additions and 194 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
uses: golangci/golangci-lint-action@v2
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.43.0
version: v1.49.0
# Optional: working directory, useful for monorepos
# working-directory: somedir

Expand Down
23 changes: 18 additions & 5 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,18 @@ linters-settings:
- '*.Test'
- '*.Test2'

exhaustruct:
# List of regular expressions to match struct packages and names.
# If this list is empty, all structs are tested.
# Default: []
include:
- '.*\.Test'
- '.*\.Test2'
# List of regular expressions to exclude struct packages and names from check.
# Default: []
# exclude:
# - 'cobra\.Command$'

forbidigo:
# Forbid the following identifiers (identifiers are written using regexp):
forbid:
Expand Down Expand Up @@ -805,15 +817,16 @@ linters:
- asciicheck
- bodyclose
- cyclop
- deadcode
#- deadcode # deprecated, replaced by unused
- depguard
- dogsled
- dupl
- durationcheck
- errcheck
- errorlint
- exhaustive
- exhaustivestruct
- exhaustruct
#- exhaustivestruct # deprecated, replaced by exhaustruct.
- exportloopref
- forbidigo
- funlen
Expand All @@ -833,7 +846,7 @@ linters:
- gosec
- gosimple
- govet
- ifshort
#- ifshort # 'ifshort' is deprecated
- importas
- ineffassign
- lll
Expand All @@ -852,14 +865,14 @@ linters:
- rowserrcheck
- sqlclosecheck
- staticcheck
- structcheck
#- structcheck # deprecated, replaced by unused
- stylecheck
- thelper
- tparallel
- unconvert
- unparam
- unused
- varcheck
#- varcheck # deprecated, replaced by unused
- wastedassign
- whitespace
- bidichk
Expand Down
3 changes: 2 additions & 1 deletion cliutil/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"context"
"errors"
"io"
"os"
"os/exec"
Expand Down Expand Up @@ -108,7 +109,7 @@ func readBuf(r *bufio.Reader, fn LoggingFunc) error {
if err != nil {
return err
}
} else if err == io.EOF {
} else if errors.Is(err, io.EOF) {
break
} else {
return err
Expand Down
95 changes: 11 additions & 84 deletions crtutil/crt.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,87 +9,25 @@ import (
)

var (
ErrNoPEMData = errors.New("no pem data is found")
ErrUnknownKeyType = errors.New("unknown private key type in PKCS#8 wrapping")
)

// ParseCertFile parses x509.Certificate from the given file.
// The data is expected to be PEM Encoded and contain one certificate
// ReadAsX509FromFile read x509.Certificate from the given file.
// The data is expected to be PEM Encoded and contain one or more certificates
// with PEM type "CERTIFICATE".
// Deprecated: use ReadFileAsX509 instead.
func ParseCertFile(fpath string) (*x509.Certificate, error) {
return ReadFileAsX509(fpath)
}

// ReadFileAsX509 read x509.Certificate from the given file.
// The data is expected to be PEM Encoded and contain one certificate
// with PEM type "CERTIFICATE".
func ReadFileAsX509(fpath string) (*x509.Certificate, error) {
bs, err := ioutil.ReadFile(fpath)
if err != nil {
return nil, err
}

return ReadBytesAsX509(bs)
}

// ParseCertBytes parses a single x509.Certificate from the given data.
// The data is expected to be PEM Encoded and contain one certificate
// with PEM type "CERTIFICATE".
// Deprecated: use ReadBytesAsX509 instead.
func ParseCertBytes(data []byte) (*x509.Certificate, error) {
return ReadBytesAsX509(data)
}

// ReadBytesAsX509 read x509.Certificate from the given data.
// The data is expected to be PEM Encoded and contain one certificate
// with PEM type "CERTIFICATE".
func ReadBytesAsX509(data []byte) (*x509.Certificate, error) {
if len(data) == 0 {
return nil, nil
}
bl, _ := pem.Decode(data)
if bl == nil {
return nil, ErrNoPEMData
}
cert, err := x509.ParseCertificate(bl.Bytes)
if err != nil {
return nil, err
}
return cert, nil
}

// ParseCertChainFile parses the x509.Certificate chain from the given file.
// The data is expected to be PEM Encoded and contain one of more certificates
// with PEM type "CERTIFICATE".
// Deprecated: use ReadChainFileAsX509 instead.
func ParseCertChainFile(fpath string) ([]*x509.Certificate, error) {
return ReadChainFileAsX509(fpath)
}

// ReadChainFileAsX509 read the x509.Certificate chain from the given file.
// The data is expected to be PEM Encoded and contain one of more certificates
// with PEM type "CERTIFICATE".
func ReadChainFileAsX509(fpath string) ([]*x509.Certificate, error) {
func ReadAsX509FromFile(fpath string) ([]*x509.Certificate, error) {
bs, err := ioutil.ReadFile(fpath)
if err != nil {
return nil, err
}
return ReadChainBytesAsX509(bs)
}

// ParseCertChainBytes parses x509.Certificate chain from the given data.
// The data is expected to be PEM Encoded and contain one of more certificates
// with PEM type "CERTIFICATE".
// Deprecated: use ReadChainBytesAsX509 instead.
func ParseCertChainBytes(data []byte) ([]*x509.Certificate, error) {
return ReadChainBytesAsX509(data)
return ReadAsX509(bs)
}

// ReadChainBytesAsX509 read x509.Certificate chain from the given data.
// ReadAsX509 read x509.Certificate chain from the given data.
// The data is expected to be PEM Encoded and contain one of more certificates
// with PEM type "CERTIFICATE".
func ReadChainBytesAsX509(data []byte) ([]*x509.Certificate, error) {
func ReadAsX509(data []byte) ([]*x509.Certificate, error) {
var (
certs []*x509.Certificate
cert *x509.Certificate
Expand All @@ -111,19 +49,9 @@ func ReadChainBytesAsX509(data []byte) ([]*x509.Certificate, error) {
certs = append(certs, cert)
}

if len(certs) == 0 {
return nil, ErrNoPEMData
}

return certs, nil
}

// CertToPEM converts a x509.Certificate into a PEM block.
// Deprecated: use EncodeX509ToPEM instead.
func CertToPEM(cert *x509.Certificate) []byte {
return EncodeX509ToPEM(cert, nil)
}

// EncodeX509ToPEM converts a x509.Certificate into a PEM block.
func EncodeX509ToPEM(cert *x509.Certificate, headers map[string]string) []byte {
return pem.EncodeToMemory(&pem.Block{
Expand All @@ -133,12 +61,6 @@ func EncodeX509ToPEM(cert *x509.Certificate, headers map[string]string) []byte {
})
}

// CertChainToPEM converts a slice of x509.Certificate into PEM block, in the order they are passed.
// Deprecated: use EncodeX509ChainToPEM instead.
func CertChainToPEM(chain []*x509.Certificate) ([]byte, error) {
return EncodeX509ChainToPEM(chain, nil)
}

// EncodeX509ChainToPEM converts a slice of x509.Certificate into PEM block, in the order they are passed.
func EncodeX509ChainToPEM(chain []*x509.Certificate, headers map[string]string) ([]byte, error) {
var buf bytes.Buffer
Expand All @@ -156,3 +78,8 @@ func EncodeX509ChainToPEM(chain []*x509.Certificate, headers map[string]string)
}
return buf.Bytes(), nil
}

// IsSelfSigned whether the given x509.Certificate is self-signed.
func IsSelfSigned(cert *x509.Certificate) bool {
return cert.CheckSignatureFrom(cert) == nil
}
Loading

0 comments on commit 0faa31d

Please sign in to comment.