10 Commits

Author SHA1 Message Date
72f6073e47 feat: use armored gpg signature
Some checks failed
Cadoles/go-http-peering/pipeline/head There was a failure building this commit
2024-01-04 11:35:30 +01:00
1bf8d755ed fix: load dek header-less private keys
Some checks reported warnings
Cadoles/go-http-peering/pipeline/head This commit is unstable
2023-11-21 14:13:32 +01:00
ced46bf6eb fix: use detached signature for release
Some checks reported warnings
Cadoles/go-http-peering/pipeline/head This commit is unstable
2023-11-21 12:21:42 +01:00
cf8f026574 feat: sign released binaries
Some checks reported warnings
Cadoles/go-http-peering/pipeline/head This commit is unstable
2023-10-19 15:44:01 +02:00
891cfa7540 Fix panic when peer is not yet associated
Some checks reported warnings
Cadoles/go-http-peering/pipeline/head This commit is unstable
2023-09-04 21:08:46 -06:00
d38f0be312 Use GOMIPS=softfloat by default and do not use UPX on mips targeted binaries
Some checks reported warnings
Cadoles/go-http-peering/pipeline/head This commit is unstable
2022-09-16 14:47:40 +02:00
5be381d2b7 Disable upx compression for mips arch
Some checks reported warnings
Cadoles/go-http-peering/pipeline/head This commit is unstable
2022-09-12 18:22:49 +02:00
7cff0e3f91 Add mipsle target
Some checks reported warnings
Cadoles/go-http-peering/pipeline/head This commit is unstable
2022-09-12 17:47:51 +02:00
f872a68906 Return wrapped errors 2022-09-12 17:46:59 +02:00
6257330dd3 Use jenkins main library version
Some checks reported warnings
Cadoles/go-http-peering/pipeline/head This commit is unstable
2022-09-05 14:40:36 +02:00
14 changed files with 88 additions and 60 deletions

2
.env.dist Normal file
View File

@ -0,0 +1,2 @@
GPG_SIGNING_KEY=
ARCH_TARGETS='amd64 arm arm64 386'

5
.gitignore vendored
View File

@ -3,4 +3,7 @@
/bin /bin
/testdata /testdata
/release /release
/out /out
/.mktools
/tools
/.env

15
Jenkinsfile vendored
View File

@ -1,4 +1,4 @@
@Library('cadoles@gitea-release') _ @Library('cadoles') _
pipeline { pipeline {
agent { agent {
@ -25,19 +25,6 @@ pipeline {
} }
} }
} }
stage('Release') {
steps {
script {
sh 'make tidy'
sh 'ARCH_TARGETS="amd64 arm arm64" make release'
def attachments = sh(returnStdout: true, script: 'find release -maxdepth 1 -type f').split(' ')
gitea.release('forge-jenkins', 'Cadoles', 'go-http-peering', [
'attachments': attachments
])
}
}
}
} }
post { post {

View File

@ -1,3 +1,5 @@
SHELL := /bin/bash
test: test:
go clean -testcache go clean -testcache
go test -cover -v ./... go test -cover -v ./...
@ -5,8 +7,11 @@ test:
watch: watch:
go run -mod=readonly github.com/cortesi/modd/cmd/modd@latest go run -mod=readonly github.com/cortesi/modd/cmd/modd@latest
release: release: tidy .env
script/release ( set -o allexport && source .env && set +o allexport && script/release )
.env:
cp .env.dist .env
tidy: tidy:
go mod tidy go mod tidy
@ -17,4 +22,26 @@ lint:
bin/keygen: bin/keygen:
CGO_ENABLED=0 go build -o bin/keygen ./cmd/keygen CGO_ENABLED=0 go build -o bin/keygen ./cmd/keygen
.PHONY: test lint doc sequence-diagram bin/keygen release .PHONY: test lint doc sequence-diagram bin/keygen release
gitea-release: .mktools tools/gitea-release/bin/gitea-release.sh release
GITEA_RELEASE_PROJECT="go-http-peering" \
GITEA_RELEASE_ORG="Cadoles" \
GITEA_RELEASE_BASE_URL="https://forge.cadoles.com" \
GITEA_RELEASE_VERSION="$(MKT_PROJECT_VERSION)" \
GITEA_RELEASE_NAME="$(MKT_PROJECT_VERSION)" \
GITEA_RELEASE_COMMITISH_TARGET="$(GIT_VERSION)" \
GITEA_RELEASE_IS_DRAFT="false" \
GITEA_RELEASE_BODY="" \
GITEA_RELEASE_ATTACHMENTS="$$(find release -type f -name '*.tar.gz')" \
tools/gitea-release/bin/gitea-release.sh
.PHONY: mktools
mktools:
rm -rf .mktools
curl -k -q https://forge.cadoles.com/Cadoles/mktools/raw/branch/master/install.sh | $(SHELL)
.mktools:
$(MAKE) mktools
-include .mktools/*.mk

View File

@ -4,20 +4,21 @@ import (
"fmt" "fmt"
"forge.cadoles.com/Cadoles/go-http-peering/crypto" "forge.cadoles.com/Cadoles/go-http-peering/crypto"
"github.com/pkg/errors"
) )
func createKey() { func createKey() {
passphrase, err := getPassphrase() passphrase, err := getPassphrase()
if err != nil { if err != nil {
handleError(err) handleError(errors.WithStack(err))
} }
key, err := crypto.CreateRSAKey(keySize) key, err := crypto.CreateRSAKey(keySize)
if err != nil { if err != nil {
handleError(err) handleError(errors.WithStack(err))
} }
privatePEM, err := crypto.EncodePrivateKeyToEncryptedPEM(key, passphrase) privatePEM, err := crypto.EncodePrivateKeyToEncryptedPEM(key, passphrase)
if err != nil { if err != nil {
handleError(err) handleError(errors.WithStack(err))
} }
fmt.Print(string(privatePEM)) fmt.Print(string(privatePEM))
} }

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"forge.cadoles.com/Cadoles/go-http-peering/crypto" "forge.cadoles.com/Cadoles/go-http-peering/crypto"
"github.com/pkg/errors"
peering "forge.cadoles.com/Cadoles/go-http-peering" peering "forge.cadoles.com/Cadoles/go-http-peering"
) )
@ -11,11 +12,11 @@ import (
func createToken() { func createToken() {
privateKey, err := loadPrivateKey() privateKey, err := loadPrivateKey()
if err != nil { if err != nil {
handleError(err) handleError(errors.WithStack(err))
} }
token, err := crypto.CreateServerToken(privateKey, tokenIssuer, peering.PeerID(tokenPeerID)) token, err := crypto.CreateServerToken(privateKey, tokenIssuer, peering.PeerID(tokenPeerID))
if err != nil { if err != nil {
handleError(err) handleError(errors.WithStack(err))
} }
fmt.Println(token) fmt.Println(token)
} }

View File

@ -4,16 +4,17 @@ import (
"fmt" "fmt"
"forge.cadoles.com/Cadoles/go-http-peering/crypto" "forge.cadoles.com/Cadoles/go-http-peering/crypto"
"github.com/pkg/errors"
) )
func getPublicKey() { func getPublicKey() {
privateKey, err := loadPrivateKey() privateKey, err := loadPrivateKey()
if err != nil { if err != nil {
handleError(err) handleError(errors.WithStack(err))
} }
publicPEM, err := crypto.EncodePublicKeyToPEM(privateKey.Public()) publicPEM, err := crypto.EncodePublicKeyToPEM(privateKey.Public())
if err != nil { if err != nil {
handleError(err) handleError(errors.WithStack(err))
} }
fmt.Print(string(publicPEM)) fmt.Print(string(publicPEM))
} }

View File

@ -6,13 +6,13 @@ import (
"crypto/rsa" "crypto/rsa"
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"syscall" "syscall"
"forge.cadoles.com/Cadoles/go-http-peering/crypto" "forge.cadoles.com/Cadoles/go-http-peering/crypto"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh/terminal" "golang.org/x/crypto/ssh/terminal"
) )
@ -29,14 +29,14 @@ func askPassphrase() ([]byte, error) {
fmt.Print("Passphrase: ") fmt.Print("Passphrase: ")
passphrase, err := terminal.ReadPassword(syscall.Stdin) passphrase, err := terminal.ReadPassword(syscall.Stdin)
if err != nil { if err != nil {
return nil, err return nil, errors.WithStack(err)
} }
fmt.Println() fmt.Println()
fmt.Print("Confirm passphrase: ") fmt.Print("Confirm passphrase: ")
passphraseConfirmation, err := terminal.ReadPassword(syscall.Stdin) passphraseConfirmation, err := terminal.ReadPassword(syscall.Stdin)
if err != nil { if err != nil {
return nil, err return nil, errors.WithStack(err)
} }
fmt.Println() fmt.Println()
@ -48,7 +48,6 @@ func askPassphrase() ([]byte, error) {
} }
func privateKeyToEncryptedPEM(key *rsa.PrivateKey, passphrase []byte) ([]byte, error) { func privateKeyToEncryptedPEM(key *rsa.PrivateKey, passphrase []byte) ([]byte, error) {
if passphrase == nil { if passphrase == nil {
return nil, errors.New("passphrase cannot be empty") return nil, errors.New("passphrase cannot be empty")
} }
@ -61,7 +60,7 @@ func privateKeyToEncryptedPEM(key *rsa.PrivateKey, passphrase []byte) ([]byte, e
block, err := x509.EncryptPEMBlock(rand.Reader, block.Type, block.Bytes, passphrase, x509.PEMCipherAES256) block, err := x509.EncryptPEMBlock(rand.Reader, block.Type, block.Bytes, passphrase, x509.PEMCipherAES256)
if err != nil { if err != nil {
return nil, err return nil, errors.WithStack(err)
} }
return pem.EncodeToMemory(block), nil return pem.EncodeToMemory(block), nil
@ -73,24 +72,24 @@ func loadPrivateKey() (*rsa.PrivateKey, error) {
} }
pem, err := ioutil.ReadFile(keyFile) pem, err := ioutil.ReadFile(keyFile)
if err != nil { if err != nil {
return nil, err return nil, errors.WithStack(err)
} }
passphrase, err := getPassphrase() passphrase, err := getPassphrase()
if err != nil { if err != nil {
return nil, err return nil, errors.WithStack(err)
} }
privateKey, err := crypto.DecodePEMEncryptedPrivateKey(pem, passphrase) privateKey, err := crypto.DecodePEMEncryptedPrivateKey(pem, passphrase)
if err != nil { if err != nil {
return nil, err return nil, errors.WithStack(err)
} }
return privateKey, nil return privateKey, nil
} }
func handleError(err error) { func handleError(err error) {
if !debug { if !debug {
fmt.Println(err) fmt.Printf("%+v\n", errors.WithStack(err))
} else { } else {
panic(err) panic(fmt.Sprintf("%+v", errors.WithStack(err)))
} }
os.Exit(1) os.Exit(1)
} }

View File

@ -6,15 +6,16 @@ import (
"crypto/rsa" "crypto/rsa"
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"errors"
jwt "github.com/dgrijalva/jwt-go" jwt "github.com/dgrijalva/jwt-go"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh"
) )
func EncodePublicKeyToPEM(key crypto.PublicKey) ([]byte, error) { func EncodePublicKeyToPEM(key crypto.PublicKey) ([]byte, error) {
pub, err := x509.MarshalPKIXPublicKey(key) pub, err := x509.MarshalPKIXPublicKey(key)
if err != nil { if err != nil {
return nil, err return nil, errors.WithStack(err)
} }
data := pem.EncodeToMemory(&pem.Block{ data := pem.EncodeToMemory(&pem.Block{
Type: "PUBLIC KEY", Type: "PUBLIC KEY",
@ -28,27 +29,23 @@ func DecodePEMToPublicKey(pem []byte) (crypto.PublicKey, error) {
} }
func DecodePEMEncryptedPrivateKey(key []byte, passphrase []byte) (*rsa.PrivateKey, error) { func DecodePEMEncryptedPrivateKey(key []byte, passphrase []byte) (*rsa.PrivateKey, error) {
var err error var (
rawKey interface{}
err error
)
// Parse PEM block if len(passphrase) == 0 {
var block *pem.Block rawKey, err = ssh.ParseRawPrivateKey(key)
if block, _ = pem.Decode(key); block == nil { } else {
return nil, errors.New("invalid PEM block") rawKey, err = ssh.ParseRawPrivateKeyWithPassphrase(key, passphrase)
} }
decryptedBlock, err := x509.DecryptPEMBlock(block, passphrase)
if err != nil { if err != nil {
return nil, err return nil, errors.WithStack(err)
}
var parsedKey interface{}
if parsedKey, err = x509.ParsePKCS1PrivateKey(decryptedBlock); err != nil {
return nil, err
} }
var privateKey *rsa.PrivateKey var privateKey *rsa.PrivateKey
var ok bool var ok bool
if privateKey, ok = parsedKey.(*rsa.PrivateKey); !ok { if privateKey, ok = rawKey.(*rsa.PrivateKey); !ok {
return nil, errors.New("invalid RSA private key") return nil, errors.New("invalid RSA private key")
} }
@ -70,7 +67,7 @@ func EncodePrivateKeyToEncryptedPEM(key *rsa.PrivateKey, passphrase []byte) ([]b
block.Bytes, passphrase, x509.PEMCipherAES256, block.Bytes, passphrase, x509.PEMCipherAES256,
) )
if err != nil { if err != nil {
return nil, err return nil, errors.WithStack(err)
} }
return pem.EncodeToMemory(block), nil return pem.EncodeToMemory(block), nil

View File

@ -8,12 +8,13 @@ import (
peering "forge.cadoles.com/Cadoles/go-http-peering" peering "forge.cadoles.com/Cadoles/go-http-peering"
jwt "github.com/dgrijalva/jwt-go" jwt "github.com/dgrijalva/jwt-go"
"github.com/pkg/errors"
) )
func CreateRSAKey(bits int) (*rsa.PrivateKey, error) { func CreateRSAKey(bits int) (*rsa.PrivateKey, error) {
key, err := rsa.GenerateKey(rand.Reader, bits) key, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil { if err != nil {
return nil, err return nil, errors.WithStack(err)
} }
return key, nil return key, nil
} }
@ -28,7 +29,7 @@ func CreateServerToken(privateKey *rsa.PrivateKey, issuer string, peerID peering
}) })
tokenStr, err := token.SignedString(privateKey) tokenStr, err := token.SignedString(privateKey)
if err != nil { if err != nil {
return "", err return "", errors.WithStack(err)
} }
return tokenStr, nil return tokenStr, nil
} }

1
go.mod
View File

@ -9,6 +9,7 @@ require (
require ( require (
github.com/google/uuid v1.0.0 // indirect github.com/google/uuid v1.0.0 // indirect
github.com/pkg/errors v0.9.1
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 // indirect golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 // indirect
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect
) )

2
go.sum
View File

@ -6,6 +6,8 @@ github.com/google/uuid v1.0.0 h1:b4Gk+7WdP/d3HZH8EJsZpvV7EtDOgaZLtnaNGIu1adA=
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/pborman/uuid v1.2.0 h1:J7Q5mO4ysT1dv8hyrUGHb9+ooztCXu1D8MY8DZYsu3g= github.com/pborman/uuid v1.2.0 h1:J7Q5mO4ysT1dv8hyrUGHb9+ooztCXu1D8MY8DZYsu3g=
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k= github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 h1:Y/gsMcFOcR+6S6f3YeMKl5g+dZMEWqcz5Czj/GWYbkM= golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 h1:Y/gsMcFOcR+6S6f3YeMKl5g+dZMEWqcz5Czj/GWYbkM=
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 h1:v6hYoSR9T5oet+pMXwUWkbiVqx/63mlHjefrHmxwfeY= golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 h1:v6hYoSR9T5oet+pMXwUWkbiVqx/63mlHjefrHmxwfeY=

View File

@ -3,7 +3,9 @@
set -eo pipefail set -eo pipefail
OS_TARGETS=(linux) OS_TARGETS=(linux)
ARCH_TARGETS=${ARCH_TARGETS:-amd64} ARCH_TARGETS=${ARCH_TARGETS:-amd64 mipsle}
export GOMIPS=softfloat
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
@ -29,10 +31,16 @@ function build {
-o "$destdir/$name" \ -o "$destdir/$name" \
"$srcdir" "$srcdir"
if [ ! -z "$(which upx)" ]; then # Disable UPX compression for MIPS archs
# See https://github.com/upx/upx/issues/339
if [ ! -z "$(which upx)" ] && [[ ! "$arch" =~ "mips" ]]; then
upx --best "$destdir/$name" upx --best "$destdir/$name"
fi fi
if [ ! -z "${GPG_SIGNING_KEY}" ]; then
echo "signing '$destdir/$name' with gpg key '$GPG_SIGNING_KEY'..."
gpg --sign --default-key "${GPG_SIGNING_KEY}" --armor --detach-sign --output "$destdir/$name.sig" "$destdir/$name"
fi
} }
function copy { function copy {

View File

@ -8,14 +8,13 @@ import (
"errors" "errors"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http"
"time" "time"
peeringCrypto "forge.cadoles.com/Cadoles/go-http-peering/crypto" peeringCrypto "forge.cadoles.com/Cadoles/go-http-peering/crypto"
peering "forge.cadoles.com/Cadoles/go-http-peering" peering "forge.cadoles.com/Cadoles/go-http-peering"
jwt "github.com/dgrijalva/jwt-go" jwt "github.com/dgrijalva/jwt-go"
"net/http"
) )
const ( const (
@ -38,7 +37,6 @@ func Authenticate(store peering.Store, key *rsa.PublicKey, funcs ...OptionFunc)
middleware := func(next http.Handler) http.Handler { middleware := func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) { fn := func(w http.ResponseWriter, r *http.Request) {
serverToken := r.Header.Get(ServerTokenHeader) serverToken := r.Header.Get(ServerTokenHeader)
if serverToken == "" { if serverToken == "" {
sendError(w, http.StatusUnauthorized) sendError(w, http.StatusUnauthorized)
@ -77,6 +75,7 @@ func Authenticate(store peering.Store, key *rsa.PublicKey, funcs ...OptionFunc)
return return
} }
sendError(w, http.StatusUnauthorized) sendError(w, http.StatusUnauthorized)
return
case ErrPeerRejected: case ErrPeerRejected:
if err := store.UpdateLastContact(serverClaims.PeerID, r.RemoteAddr, time.Now()); err != nil { if err := store.UpdateLastContact(serverClaims.PeerID, r.RemoteAddr, time.Now()); err != nil {
logger.Printf("[ERROR] %s", err) logger.Printf("[ERROR] %s", err)
@ -115,7 +114,6 @@ func Authenticate(store peering.Store, key *rsa.PublicKey, funcs ...OptionFunc)
r.Body = ioutil.NopCloser(bytes.NewBuffer(body)) r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
} }
return http.HandlerFunc(fn) return http.HandlerFunc(fn)
} }