Skip to content

Commit

Permalink
add support for reading certificates from clipboard. (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhadfield committed Jul 5, 2023
1 parent 387ab58 commit 46c2488
Show file tree
Hide file tree
Showing 134 changed files with 46,370 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ permissions:
contents: write

jobs:
prereqs:
runs-on: ubuntu-latest
steps:
- name: Install libx11-dev
run: sudo apt-get update && sudo apt-get install -y libx11-dev
go:
needs:
- prereqs
uses: pete911/github-actions/.github/workflows/go.yml@main
go-release:
needs:
Expand Down
16 changes: 16 additions & 0 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"os"
"strconv"

"golang.design/x/clipboard"
)

type Flags struct {
Expand All @@ -17,6 +19,7 @@ type Flags struct {
Pem bool
PemOnly bool
Version bool
Clipboard bool
Args []string
}

Expand All @@ -38,6 +41,10 @@ func ParseFlags() (Flags, error) {
"whether to print pem as well")
flagSet.BoolVar(&flags.PemOnly, "pem-only", getBoolEnv("CERTINFO_PEM_ONLY", false),
"whether to print only pem (useful for downloading certs from host)")
if isClipboardSupported() {
flagSet.BoolVar(&flags.Clipboard, "clipboard", false,
"read input from clipboard")
}
flagSet.BoolVar(&flags.Version, "version", getBoolEnv("CERTINFO_VERSION", false),
"certinfo version")

Expand Down Expand Up @@ -66,3 +73,12 @@ func getBoolEnv(envName string, defaultValue bool) bool {
}
return defaultValue
}

func isClipboardSupported() bool {

if err := clipboard.Init(); err == nil {
return true
}

return false
}
37 changes: 35 additions & 2 deletions flag_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main

import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestParseFlags(t *testing.T) {
Expand All @@ -23,6 +24,7 @@ func TestParseFlags(t *testing.T) {
assert.False(t, flags.PemOnly)
assert.False(t, flags.Version)
assert.Empty(t, flags.Args)
assert.False(t, flags.Clipboard)
})

t.Run("given args are set and env vars empty then flags are set to provided args", func(t *testing.T) {
Expand All @@ -49,6 +51,37 @@ func TestParseFlags(t *testing.T) {
assert.Empty(t, flags.Args)
})

t.Run("given clipboard supported and args are set and env vars empty then flags are set to provided args", func(t *testing.T) {

if !isClipboardSupported() {
t.Skip("clipboard not supported in this environment")
}

setInput(t, []string{"flag",
"-expiry=true",
"-insecure=true",
"-chains=true",
"-chains=true",
"-pem=true",
"-pem-only=true",
"-version=true",
"-clipboard=true",
}, nil)

flags, err := ParseFlags()
require.NoError(t, err)

assert.True(t, flags.Expiry)
assert.True(t, flags.Insecure)
assert.True(t, flags.Chains)
assert.True(t, flags.Pem)
assert.True(t, flags.PemOnly)
assert.True(t, flags.Version)
assert.True(t, flags.Version)
assert.Empty(t, flags.Args)
assert.True(t, flags.Clipboard)
})

t.Run("given args are not set and env vars are set then flags are set to provided env vars", func(t *testing.T) {

setInput(t, []string{"flag"}, map[string]string{
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ go 1.20
require (
github.com/icza/gox v0.0.0-20230330130131-23e1aaac139e
github.com/stretchr/testify v1.8.4
golang.design/x/clipboard v0.7.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect
golang.org/x/image v0.6.0 // indirect
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c // indirect
golang.org/x/sys v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
49 changes: 49 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/icza/gox v0.0.0-20230330130131-23e1aaac139e h1:aIs1rPxsH8t12+eWzEfrvK2o99fwiC0P9Qr8roW0vsU=
Expand All @@ -6,6 +7,54 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.design/x/clipboard v0.7.0 h1:4Je8M/ys9AJumVnl8m+rZnIvstSnYj1fvzqYrU3TXvo=
golang.design/x/clipboard v0.7.0/go.mod h1:PQIvqYO9GP29yINEfsEn5zSQKAz3UgXmZKzDA6dnq2E=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 h1:estk1glOnSVeJ9tdEZZc5mAMDZk5lNJNyJ6DvrBkTEU=
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.6.0 h1:bR8b5okrPI3g/gyZakLZHeWxAR8Dn5CyxXv1hLH5g/4=
golang.org/x/image v0.6.0/go.mod h1:MXLdDR43H7cDJq5GEGXEVeeNhPgi+YYEQ2pC1byI1x0=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c h1:Gk61ECugwEHL6IiyyNLXNzmu8XslmRP2dS0xjIYhbb4=
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c/go.mod h1:aAjjkJNdrh3PMckS4B10TGS2nag27cbKR1y2BpUxsiY=
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
12 changes: 10 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package main

import (
"fmt"
"github.com/pete911/certinfo/pkg/cert"
"os"
"strconv"
"strings"

"github.com/pete911/certinfo/pkg/cert"
)

var Version = "dev"
Expand Down Expand Up @@ -44,6 +43,15 @@ func main() {

func LoadCertificatesLocations(flags Flags) cert.CertificateLocations {

if flags.Clipboard {
certificateLocation, err := cert.LoadCertificateFromClipboard()
if err != nil {
printCertFileError("clipboard", err)
return nil
}
return []cert.CertificateLocation{certificateLocation}
}

if len(flags.Args) > 0 {
var certificateLocations cert.CertificateLocations
for _, arg := range flags.Args {
Expand Down
16 changes: 16 additions & 0 deletions pkg/cert/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package cert
import (
"bytes"
"crypto/tls"
"errors"
"fmt"
"golang.design/x/clipboard"
"io"
"net"
"os"
Expand Down Expand Up @@ -92,6 +94,20 @@ func LoadCertificateFromStdin() (CertificateLocation, error) {
return loadCertificate("stdin", content)
}

func LoadCertificateFromClipboard() (CertificateLocation, error) {

if err := clipboard.Init(); err != nil {
return CertificateLocation{}, fmt.Errorf("unable to load from clipboard: %w", err)
}

content := clipboard.Read(clipboard.FmtText)
if content == nil {
return CertificateLocation{}, errors.New("clipboard is empty")
}

return loadCertificate("clipboard", content)
}

func loadCertificate(fileName string, data []byte) (CertificateLocation, error) {

certificates, err := FromBytes(bytes.TrimSpace(data))
Expand Down
18 changes: 17 additions & 1 deletion pkg/cert/location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package cert
import (
"bytes"
"crypto/tls"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
"golang.design/x/clipboard"
)

func Test_nameFormat(t *testing.T) {
Expand Down Expand Up @@ -39,3 +41,17 @@ func Test_loadCertificate(t *testing.T) {
require.NoError(t, err)
})
}

func Test_loadCertificateFromClipboard(t *testing.T) {
if err := clipboard.Init(); err != nil {
t.Skip("clipboard not supported in this environment")
}

t.Run("given valid certificate in clipboard then cert is loaded", func(t *testing.T) {
certificate := loadTestFile(t, "cert.pem")
clipboard.Write(clipboard.FmtText, certificate)

_, err := LoadCertificateFromClipboard()
require.NoError(t, err)
})
}
15 changes: 15 additions & 0 deletions vendor/golang.design/x/clipboard/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
13 changes: 13 additions & 0 deletions vendor/golang.design/x/clipboard/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2021 The golang.design Initiative Authors.
# All rights reserved. Use of this source code is governed
# by a MIT license that can be found in the LICENSE file.
#
# Written by Changkun Ou <changkun.de>

FROM golang:1.17
RUN apt-get update && apt-get install -y \
xvfb libx11-dev libegl1-mesa-dev libgles2-mesa-dev \
&& apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
WORKDIR /app
COPY . .
CMD [ "sh", "-c", "./tests/test-docker.sh" ]
21 changes: 21 additions & 0 deletions vendor/golang.design/x/clipboard/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Changkun Ou <contact@changkun.de>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading

0 comments on commit 46c2488

Please sign in to comment.