1 Commits

Author SHA1 Message Date
e6727c83f1 Add Jenkinsfile
All checks were successful
Cadoles/go-http-peering/pipeline/head This commit looks good
2022-09-05 12:24:32 +02:00
15 changed files with 93 additions and 113 deletions

View File

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

5
.gitignore vendored
View File

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

28
Jenkinsfile vendored
View File

@ -1,4 +1,4 @@
@Library('cadoles') _ @Library('cadoles@gitea-release') _
pipeline { pipeline {
agent { agent {
@ -9,19 +9,25 @@ pipeline {
} }
stages { stages {
stage('Lint') { stage('Release') {
steps { steps {
script { script {
catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') { def isPrerelease = true
sh 'make lint' try {
sh(script: "git describe --exact-match ${GIT_COMMIT}")
isPrerelease = false
} catch (err) {
println "Could not find tag associated with commit '${GIT_COMMIT}' ! Generating a prerelease..."
} }
}
} sh 'ARCH_TARGETS="amd64 arm arm64" make release'
}
stage('Test') { def attachments = sh(returnStdout: true, script: 'ls release/*.tar.gz').split(' ')
steps { gitea.release('forge-jenkins', 'Cadoles', 'go-http-peering', [
script { 'attachments': attachments,
sh 'make test' 'isDraft': false,
'isPrerelease': isPrerelease,
])
} }
} }
} }

View File

@ -1,47 +1,35 @@
SHELL := /bin/bash
test: test:
go clean -testcache go clean -testcache
go test -cover -v ./... go test -cover -v ./...
watch: watch:
go run -mod=readonly github.com/cortesi/modd/cmd/modd@latest modd
release: tidy .env release:
( set -o allexport && source .env && set +o allexport && script/release ) script/release
.env: deps:
cp .env.dist .env GO111MODULE=off go get -u golang.org/x/tools/cmd/godoc
GO111MODULE=off go get -u github.com/cortesi/modd/cmd/modd
GO111MODULE=off go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
GO111MODULE=off go get -u github.com/lmika/goseq
tidy: tidy:
go mod tidy go mod tidy
lint: lint:
go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.49.0 run --tests=false --enable-all golangci-lint run --tests=false --enable-all
sequence-diagram: sd-advertise sd-update sd-ping
sd-%:
goseq doc/sequence-diagram/$*.seq > doc/sequence-diagram/$*.svg
doc:
@echo "open your browser to http://localhost:6060/pkg/forge.cadoles.com/Cadoles/go-http-peering to see the documentation"
godoc -http=:6060
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,21 +4,20 @@ 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(errors.WithStack(err)) handleError(err)
} }
key, err := crypto.CreateRSAKey(keySize) key, err := crypto.CreateRSAKey(keySize)
if err != nil { if err != nil {
handleError(errors.WithStack(err)) handleError(err)
} }
privatePEM, err := crypto.EncodePrivateKeyToEncryptedPEM(key, passphrase) privatePEM, err := crypto.EncodePrivateKeyToEncryptedPEM(key, passphrase)
if err != nil { if err != nil {
handleError(errors.WithStack(err)) handleError(err)
} }
fmt.Print(string(privatePEM)) fmt.Print(string(privatePEM))
} }

View File

@ -4,7 +4,6 @@ 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"
) )
@ -12,11 +11,11 @@ import (
func createToken() { func createToken() {
privateKey, err := loadPrivateKey() privateKey, err := loadPrivateKey()
if err != nil { if err != nil {
handleError(errors.WithStack(err)) handleError(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(errors.WithStack(err)) handleError(err)
} }
fmt.Println(token) fmt.Println(token)
} }

View File

@ -4,17 +4,16 @@ 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(errors.WithStack(err)) handleError(err)
} }
publicPEM, err := crypto.EncodePublicKeyToPEM(privateKey.Public()) publicPEM, err := crypto.EncodePublicKeyToPEM(privateKey.Public())
if err != nil { if err != nil {
handleError(errors.WithStack(err)) handleError(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, errors.WithStack(err) return nil, 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, errors.WithStack(err) return nil, err
} }
fmt.Println() fmt.Println()
@ -48,6 +48,7 @@ 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")
} }
@ -60,7 +61,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, errors.WithStack(err) return nil, err
} }
return pem.EncodeToMemory(block), nil return pem.EncodeToMemory(block), nil
@ -72,24 +73,24 @@ func loadPrivateKey() (*rsa.PrivateKey, error) {
} }
pem, err := ioutil.ReadFile(keyFile) pem, err := ioutil.ReadFile(keyFile)
if err != nil { if err != nil {
return nil, errors.WithStack(err) return nil, err
} }
passphrase, err := getPassphrase() passphrase, err := getPassphrase()
if err != nil { if err != nil {
return nil, errors.WithStack(err) return nil, err
} }
privateKey, err := crypto.DecodePEMEncryptedPrivateKey(pem, passphrase) privateKey, err := crypto.DecodePEMEncryptedPrivateKey(pem, passphrase)
if err != nil { if err != nil {
return nil, errors.WithStack(err) return nil, err
} }
return privateKey, nil return privateKey, nil
} }
func handleError(err error) { func handleError(err error) {
if !debug { if !debug {
fmt.Printf("%+v\n", errors.WithStack(err)) fmt.Println(err)
} else { } else {
panic(fmt.Sprintf("%+v", errors.WithStack(err))) panic(err)
} }
os.Exit(1) os.Exit(1)
} }

View File

@ -6,16 +6,15 @@ 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, errors.WithStack(err) return nil, err
} }
data := pem.EncodeToMemory(&pem.Block{ data := pem.EncodeToMemory(&pem.Block{
Type: "PUBLIC KEY", Type: "PUBLIC KEY",
@ -29,23 +28,27 @@ 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 ( var err error
rawKey interface{}
err error
)
if len(passphrase) == 0 { // Parse PEM block
rawKey, err = ssh.ParseRawPrivateKey(key) var block *pem.Block
} else { if block, _ = pem.Decode(key); block == nil {
rawKey, err = ssh.ParseRawPrivateKeyWithPassphrase(key, passphrase) return nil, errors.New("invalid PEM block")
} }
decryptedBlock, err := x509.DecryptPEMBlock(block, passphrase)
if err != nil { if err != nil {
return nil, errors.WithStack(err) return nil, 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 = rawKey.(*rsa.PrivateKey); !ok { if privateKey, ok = parsedKey.(*rsa.PrivateKey); !ok {
return nil, errors.New("invalid RSA private key") return nil, errors.New("invalid RSA private key")
} }
@ -67,7 +70,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, errors.WithStack(err) return nil, err
} }
return pem.EncodeToMemory(block), nil return pem.EncodeToMemory(block), nil

View File

@ -8,13 +8,12 @@ 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, errors.WithStack(err) return nil, err
} }
return key, nil return key, nil
} }
@ -29,7 +28,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 "", errors.WithStack(err) return "", err
} }
return tokenStr, nil return tokenStr, nil
} }

13
go.mod
View File

@ -1,17 +1,12 @@
module forge.cadoles.com/Cadoles/go-http-peering module forge.cadoles.com/Cadoles/go-http-peering
require ( require (
github.com/davecgh/go-spew v1.1.1
github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/go-chi/chi v3.3.3+incompatible github.com/go-chi/chi v3.3.3+incompatible
github.com/pborman/uuid v1.2.0 github.com/pborman/uuid v1.2.0
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 // indirect
) )
require ( go 1.13
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/term v0.0.0-20220722155259-a9ba230a4035 // indirect
)
go 1.18

14
go.sum
View File

@ -1,3 +1,5 @@
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/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/go-chi/chi v3.3.3+incompatible h1:KHkmBEMNkwKuK4FdQL7N2wOeB9jnIx7jR5wsuSBEFI8= github.com/go-chi/chi v3.3.3+incompatible h1:KHkmBEMNkwKuK4FdQL7N2wOeB9jnIx7jR5wsuSBEFI8=
@ -6,11 +8,7 @@ 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= golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2 h1:NwxKRvbkH5MsNkvOtPZi3/3kmI8CAzs3mtv+GLQMkNo=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 h1:Y/gsMcFOcR+6S6f3YeMKl5g+dZMEWqcz5Czj/GWYbkM= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 h1:v6hYoSR9T5oet+pMXwUWkbiVqx/63mlHjefrHmxwfeY=
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 h1:Q5284mrmYTpACcm+eAKjKJH48BBwSyfJqmmGDTtT8Vc=
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=

View File

@ -2,4 +2,8 @@
!vendor/**.go { !vendor/**.go {
prep: make test prep: make test
prep: make bin/keygen prep: make bin/keygen
}
doc/sequence-diagram/*.seq {
prep: make sequence-diagram
} }

View File

@ -3,9 +3,7 @@
set -eo pipefail set -eo pipefail
OS_TARGETS=(linux) OS_TARGETS=(linux)
ARCH_TARGETS=${ARCH_TARGETS:-amd64 mipsle} ARCH_TARGETS=${ARCH_TARGETS:-amd64}
export GOMIPS=softfloat
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
@ -31,16 +29,10 @@ function build {
-o "$destdir/$name" \ -o "$destdir/$name" \
"$srcdir" "$srcdir"
# Disable UPX compression for MIPS archs if [ ! -z "$(which upx)" ]; then
# 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,13 +8,14 @@ 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 (
@ -37,6 +38,7 @@ 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)
@ -75,7 +77,6 @@ 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)
@ -114,6 +115,7 @@ 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)
} }