Skip to content

Commit

Permalink
resolve merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
MayankMittal1 committed Dec 7, 2021
1 parent e148114 commit d3990a5
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 8 deletions.
1 change: 1 addition & 0 deletions config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ manifests = [
"elasticsearch_statefulset.yaml",
"kibana.yaml",
"fluentd-daemonset.yaml",
"ingress.yml"
]

[services.api]
Expand Down
55 changes: 55 additions & 0 deletions lib/utils/ssh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package utils

import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"golang.org/x/crypto/ssh"
"strings"
)

func CheckPrivateKey(privateKey, publicKey string) bool {
//check key
pub, _, _, _, err := ssh.ParseAuthorizedKey([]byte(publicKey))
if err != nil {
fmt.Println("error in parsing public key")
return false
}

private, err := ssh.ParsePrivateKey([]byte(privateKey))
if err != nil {
fmt.Println("error in parsing private key")
return false
}

return bytes.Equal(private.PublicKey().Marshal(), pub.Marshal())
}

func GenerateSSHKeyPair() (string, string, error) {
privateKey, err := rsa.GenerateKey(rand.Reader, 1024)
if err != nil {
return "", "", err
}

// generate and write private key as PEM
var privKeyBuf strings.Builder

privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
if err := pem.Encode(&privKeyBuf, privateKeyPEM); err != nil {
return "", "", err
}

// generate and write public key
pub, err := ssh.NewPublicKey(&privateKey.PublicKey)
if err != nil {
return "", "", err
}

var pubKeyBuf strings.Builder
pubKeyBuf.Write(ssh.MarshalAuthorizedKey(pub))

return pubKeyBuf.String(), privKeyBuf.String(), nil
}
22 changes: 19 additions & 3 deletions services/sshproviderservice/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sshproviderservice

import (
"bytes"
"fmt"
"log"
"net"
Expand Down Expand Up @@ -66,11 +67,26 @@ func passwordHandler(s ssh.Context, password string) bool {
return utils.CompareHashWithPassword(team.Password, password)
}

func publicKeyHandler(s ssh.Context, key ssh.PublicKey) bool {
team, err := mongo.FetchSingleTeam(s.User())
if err != nil {
return false
}

publicKey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(team.PublicKey))
if err != nil {
return false
}

return bytes.Equal(publicKey.Marshal(), key.Marshal())
}

func Server() *ssh.Server {
return &ssh.Server{
Addr: net.JoinHostPort(g.SSHProviderConfig.Host, fmt.Sprintf("%d", g.SSHProviderConfig.Port)),
Handler: sessionHandler,
PasswordHandler: passwordHandler,
Addr: net.JoinHostPort(g.SSHProviderConfig.Host, fmt.Sprintf("%d", g.SSHProviderConfig.Port)),
Handler: sessionHandler,
PasswordHandler: passwordHandler,
PublicKeyHandler: publicKeyHandler,
}
}

Expand Down
11 changes: 6 additions & 5 deletions types/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ type AdminUser struct {
}

type CTFTeam struct {
Index int `json:"id" bson:"password" binding:"required"`
Name string `json:"name" bson:"username" binding:"required"`
PodName string `json:"podname" bson:"podname" binding:"required"`
Password string `json:"password" bson:"password" binding:"required"`
Score int `json:"score" bson:"score" binding:"required"`
Index int `json:"id" bson:"password" binding:"required"`
Name string `json:"name" bson:"username" binding:"required"`
PodName string `json:"podname" bson:"podname" binding:"required"`
Password string `json:"password" bson:"password" binding:"required"`
PublicKey string `json:"publicKey" bson:"publicKey" binding:"required"` // TODO : initialize
Score int `json:"score" bson:"score" binding:"required"`
}

type Challenge struct {
Expand Down

0 comments on commit d3990a5

Please sign in to comment.