Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enterprise - Support reading a signed file with details of Enterprise license #3824

Merged
merged 36 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
b0f67ae
Add a flag in Zero to read the enterprise file and read its contents …
pawanrawal Aug 14, 2019
706746d
Add a EnterpriseEnabled field to MembershipState and propogate it to …
pawanrawal Aug 14, 2019
ce2b931
Dummy commit
pawanrawal Aug 14, 2019
69871db
Remove useless print statements.
pawanrawal Aug 14, 2019
5dd9a02
Check for max allowed nodes while calling Connect in Zero
pawanrawal Aug 14, 2019
c7bff37
Remove some TODOs which we don't need to worry about.
pawanrawal Aug 14, 2019
c880a3b
Verify signature from the file with PGP message containing data and s…
pawanrawal Aug 16, 2019
9f0fe3e
Add another small comment.
pawanrawal Aug 16, 2019
6bfb453
Propose enterprise state to the Zero cluster.
pawanrawal Aug 16, 2019
6eda1a5
Read expiry from state in Zero
pawanrawal Aug 16, 2019
80b9c11
Simplify code and consolidate Enterprise struct.
pawanrawal Aug 16, 2019
bbc98fc
Remove unnecessary locks.
pawanrawal Aug 16, 2019
41fa1c6
Remove some TODOs
pawanrawal Aug 16, 2019
2a4949d
Reduce ticker timer to 5sec.
pawanrawal Aug 16, 2019
23ca13b
Address majority of the comments by pullrequests folks.
pawanrawal Aug 18, 2019
08809dd
Add some tests for the enterprise details function.
pawanrawal Aug 19, 2019
27ca7ee
Add a HTTP endpoint which allows proposing enterprise license.
pawanrawal Aug 19, 2019
de2beaf
Handle some TODOs
pawanrawal Aug 19, 2019
8f06a59
Check EnterpriseEnabled flag in login handler.
pawanrawal Aug 19, 2019
1f7d3f6
Check MaxNodes before applying enterprise proposal.
pawanrawal Aug 19, 2019
a2e5669
Remove enterprise_license flag as we have the hhtp endpoint for it.
pawanrawal Aug 20, 2019
2c277d3
Check number of nodes before proposing enterprise license.
pawanrawal Aug 20, 2019
277f10b
Rename enterprise to license and entity to user
pawanrawal Aug 20, 2019
2fa8fb3
Rename e to l
pawanrawal Aug 20, 2019
a538d66
Refactor Connect function and also have public key as part of the code.
pawanrawal Aug 20, 2019
5b223dc
Modify the error a bit.
pawanrawal Aug 20, 2019
fd1be0d
Remove enterprise_features flag from docker_compose files.
pawanrawal Aug 20, 2019
b5dcda7
Apply license in restartCluster
pawanrawal Aug 20, 2019
85f85ca
Add some comments to the function as well.
pawanrawal Aug 20, 2019
c5bb124
Print out dummy_var
pawanrawal Aug 20, 2019
a053cd8
Remove DUMMY_VAR
pawanrawal Aug 20, 2019
04b4641
Add the correct Dgraph public key. Tests would start failing now.
pawanrawal Aug 20, 2019
101e1c0
Apply license valid for 30 days after proposing cid
pawanrawal Aug 20, 2019
09969a0
Revert changes to functions.sh
pawanrawal Aug 20, 2019
68064d6
Merge branch 'master' into pawan/license-file
pawanrawal Aug 20, 2019
ba7f3fb
Print info logs if license is about to expire in less than a week.
pawanrawal Aug 21, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ type options struct {
NumAlphas int
NumReplicas int
LruSizeMB int
Enterprise bool
AclSecret string
DataDir string
DataVol bool
Expand Down Expand Up @@ -215,17 +214,14 @@ func getAlpha(idx int) service {
if opts.WhiteList {
svc.Command += " --whitelist=10.0.0.0/8,172.16.0.0/12,192.168.0.0/16"
}
if opts.Enterprise {
svc.Command += " --enterprise_features"
if opts.AclSecret != "" {
svc.Command += " --acl_secret_file=/secret/hmac --acl_access_ttl 3s --acl_cache_ttl 5s"
svc.Volumes = append(svc.Volumes, volume{
Type: "bind",
Source: opts.AclSecret,
Target: "/secret/hmac",
ReadOnly: true,
})
}
if opts.AclSecret != "" {
svc.Command += " --acl_secret_file=/secret/hmac --acl_access_ttl 3s --acl_cache_ttl 5s"
svc.Volumes = append(svc.Volumes, volume{
Type: "bind",
Source: opts.AclSecret,
Target: "/secret/hmac",
ReadOnly: true,
})
}

return svc
Expand Down Expand Up @@ -357,8 +353,6 @@ func main() {
"mount a docker volume as /data in containers")
cmd.PersistentFlags().StringVarP(&opts.DataDir, "data_dir", "d", "",
"mount a host directory as /data in containers")
cmd.PersistentFlags().BoolVarP(&opts.Enterprise, "enterprise", "e", false,
"enable enterprise features in alphas")
cmd.PersistentFlags().StringVar(&opts.AclSecret, "acl_secret", "",
"enable ACL feature with specified HMAC secret file")
cmd.PersistentFlags().BoolVarP(&opts.UserOwnership, "user", "u", false,
Expand Down Expand Up @@ -408,10 +402,6 @@ func main() {
if opts.LruSizeMB < 1024 {
fatal(errors.Errorf("LRU cache size must be >= 1024 MB"))
}
if opts.AclSecret != "" && !opts.Enterprise {
warning("adding --enterprise because it is required by ACL feature")
opts.Enterprise = true
}
if opts.DataVol && opts.DataDir != "" {
fatal(errors.Errorf("only one of --data_vol and --data_dir may be used at a time"))
}
Expand Down
7 changes: 3 additions & 4 deletions dgraph/cmd/alpha/admin_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ func backupHandler(w http.ResponseWriter, r *http.Request) {
if !handlerInit(w, r, http.MethodPost) {
return
}
if !Alpha.Conf.GetBool("enterprise_features") {
x.SetStatus(w,
"You must enable Dgraph enterprise features first. "+
"Restart Dgraph Alpha with --enterprise_features",
if !worker.EnterpriseEnabled() {
x.SetStatus(w, "You must enable enterprise features first. "+
"Supply the appropriate license file to Dgraph Zero using the HTTP endpoint.",
"Backup failed.")
return
}
Expand Down
7 changes: 0 additions & 7 deletions dgraph/cmd/alpha/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ they form a Raft group and provide synchronous replication.
// with the flag name so that the values are picked up by Cobra/Viper's various config inputs
// (e.g, config file, env vars, cli flags, etc.)
flag := Alpha.Cmd.Flags()
flag.Bool("enterprise_features", false, "Enable Dgraph enterprise features. "+
"If you set this to true, you agree to the Dgraph Community License.")
flag.StringP("postings", "p", "p", "Directory to store posting lists.")

// Options around how to set up Badger.
Expand Down Expand Up @@ -435,11 +433,6 @@ func run() {

secretFile := Alpha.Conf.GetString("acl_secret_file")
if secretFile != "" {
if !Alpha.Conf.GetBool("enterprise_features") {
glog.Fatalf("You must enable Dgraph enterprise features with the " +
"--enterprise_features option in order to use ACL.")
}

hmacSecret, err := ioutil.ReadFile(secretFile)
if err != nil {
glog.Fatalf("Unable to read HMAC secret from file: %v", secretFile)
Expand Down
34 changes: 34 additions & 0 deletions dgraph/cmd/zero/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package zero

import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net"
"net/http"
"strconv"
Expand Down Expand Up @@ -232,6 +234,38 @@ func (st *state) getState(w http.ResponseWriter, r *http.Request) {
}
}

// applyEnterpriseLicense accepts a PGP message as a POST request body, verifies that it was
// signed using our private key and applies the license which has maxNodes and Expiry to the
// cluster.
func (st *state) applyEnterpriseLicense(w http.ResponseWriter, r *http.Request) {
x.AddCorsHeaders(w)
if r.Method == "OPTIONS" {
return
}
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusBadRequest)
x.SetStatus(w, x.ErrorInvalidMethod, "Invalid method")
return
}

w.Header().Set("Content-Type", "application/json")
b, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
x.SetStatus(w, x.ErrorInvalidRequest, err.Error())
return
}

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
if err := st.zero.applyEnterpriseLicense(ctx, bytes.NewReader(b)); err != nil {
w.WriteHeader(http.StatusBadRequest)
x.SetStatus(w, x.ErrorInvalidRequest, err.Error())
return
}
x.SetStatus(w, x.Success, "Done")
}

func (st *state) serveHTTP(l net.Listener) {
srv := &http.Server{
ReadTimeout: 10 * time.Second,
Expand Down
126 changes: 126 additions & 0 deletions dgraph/cmd/zero/pgp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright 2017-2018 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package zero

import (
"encoding/json"
"io"
"io/ioutil"

"github.com/pkg/errors"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/armor"
)

const publicKey = `-----BEGIN PGP PUBLIC KEY BLOCK-----

mQINBF1bQAwBEACe+uIPgsfTmgLVDlJhdfzUH+ff774fn/Lqf0kLactHR8I6yI3h
JO6i47IhM45VJLY0ZzXntCaItavm35NGdVuA3yPJv7YkSLTPkg5D2VHyZknb52lD
JQbtyuBQK+OZiRfekbZtAfKOljFyPxr1d9Vdw0H4jYRjNK1k3iGERUf8254Y0Wqx
wz+iMLXxlDcWnq0VBSjs+bQqr61iViIIC1S1vHKsl2Sk0QBMjYrTqyttJbGQOy00
tCMy7ZFMIIEJz8Fg0XiY4d2cmIJlvRoxVpaTWE+W9wxssR4ZqOhLGUAnermScKDc
2aTERdDhG30oW/c8KLXpCKzcUc8IEETeMcBhWRzxgi1CcQEk9KhwfBQdezvY2PyE
EjhOoFZ8ryWCrOnlNgzSnFPtohbx8VD+HctJZ5foaq5ceH+YvH5zasBG/plQXO5A
hwAc8BhGdP4jvFUIBOUyjGHlj7UcqKSDDm2uIV9XjoRfCKPav62VQKRSJXvlBdZe
2uGxgZJ6TmgI2bHa0uQn5kDdQ7CYT9NYu+qXNVNxRZ8w5eTIeDxRIeAas8G5i7eO
dzEV47wN6CkK/8vVu9vXbfiGk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pOlt/k/AjNCddnLZ00A34PhjY+sDftpWaC9uK0sikeN43
R+lNMJ39xejsFUWSJM3HmnELs4JAg/DwZ0kiS6/ffKFoXi771PuOcJpNxcYG5y3I
k09Ao2v2RwWQayli/ysAENStfiWS/fVl5tlDaYGDqF0G9haMA1XPnptrgg4S3ADx
E4Hf9ymxdCLfuVsJ0dPkqv/nWsEMIVQmFVZvWs8iz8JR7Wh6/L1KJ+HpxekqoZgq
836PkLFlKGgKJw2nP5lDJIpst/qnf8hzyGQUJnjiVh3SWNpIvH8Zhrz2BQtgJhUF
43jJL0ZpKmjIPPYbx+4TjyF8T5cSCvE=
=wx6r
-----END PGP PUBLIC KEY BLOCK-----`

// verifySignature verifies the signature given a public key. It also JSON unmarshals the details
// of the license and stores them in l.
func verifySignature(signedFile, publicKey io.Reader, l *license) error {
entityList, err := openpgp.ReadArmoredKeyRing(publicKey)
if err != nil {
return errors.Wrapf(err, "while reading public key")
}

// The signed file is expected to be have ASCII encoding, so we have to decode it before
// reading.
b, err := armor.Decode(signedFile)
if err != nil {
return errors.Wrapf(err, "while decoding license file")
}

md, err := openpgp.ReadMessage(b.Body, entityList, nil, nil)
if err != nil {
return errors.Wrapf(err, "while reading PGP message from license file")
}

// We need to read the body for the signature verification check to happen.
// md.Signature would be non-nil after reading the body if the verification is successfull.
buf, err := ioutil.ReadAll(md.UnverifiedBody)
if err != nil {
return errors.Wrapf(err, "while reading body from signed license file")
}
// This could be nil even if signature verification failed, so we also check Signature == nil
// below.
if md.SignatureError != nil {
return errors.Wrapf(md.SignatureError,
"signature error while trying to verify license file")
}
if md.Signature == nil {
return errors.New("invalid signature while trying to verify license file")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not positive, but looks like maybe you could wrap the error from md.SignatureError here if you want extra context on the failure. https://godoc.org/golang.org/x/crypto/openpgp#MessageDetails

}

err = json.Unmarshal(buf, l)
if err != nil {
return errors.Wrapf(err, "while JSON unmarshaling body of license file")
}
if l.User == "" || l.MaxNodes == 0 || l.Expiry.IsZero() {
return errors.Errorf("invalid JSON data, fields shouldn't be zero: %+v\n", l)
}
return nil
}
124 changes: 124 additions & 0 deletions dgraph/cmd/zero/pgp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package zero

import (
"bytes"
"crypto"
"testing"
"time"

"github.com/stretchr/testify/require"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/armor"
"golang.org/x/crypto/openpgp/packet"
)

func encodePublicKey(t *testing.T, e *openpgp.Entity) *bytes.Buffer {
b := new(bytes.Buffer)
encodedKeyBuf, err := armor.Encode(b, openpgp.PublicKeyType, nil)
require.NoError(t, err)
err = e.Serialize(encodedKeyBuf)
require.NoError(t, err)
err = encodedKeyBuf.Close()
require.NoError(t, err)
return b
}

func signAndWriteMessage(t *testing.T, entity *openpgp.Entity, json string) *bytes.Buffer {
b := new(bytes.Buffer)
w, err := openpgp.Sign(b, entity, nil, &packet.Config{
RSABits: 4096,
DefaultHash: crypto.SHA512,
})
require.NoError(t, err)

_, err = w.Write([]byte(json))
require.NoError(t, err)

err = w.Close()
require.NoError(t, err)

// armor encode the message
abuf := new(bytes.Buffer)
w, err = armor.Encode(abuf, "PGP MESSAGE", nil)
_, err = w.Write(b.Bytes())
require.NoError(t, err)

err = w.Close()
require.NoError(t, err)

return abuf
}

func TestEnterpriseDetails(t *testing.T) {
correctEntity, err := openpgp.NewEntity("correct", "", "correct@correct.com", &packet.Config{
RSABits: 4096,
DefaultHash: crypto.SHA512,
})

require.NoError(t, err)
incorrectEntity, err := openpgp.NewEntity("incorrect", "", "incorrect@incorrect.com", &packet.Config{
RSABits: 4096,
DefaultHash: crypto.SHA512,
})
require.NoError(t, err)
correctJSON := `{"user": "user", "max_nodes": 10, "expiry": "2019-08-16T19:09:06+10:00"}`
correctTime, err := time.Parse(time.RFC3339, "2019-08-16T19:09:06+10:00")
require.NoError(t, err)

var tests = []struct {
name string
signingEntity *openpgp.Entity
json string
verifyingEntity *openpgp.Entity
expectError bool
expectedOutput license
}{
{
"Signing JSON with empty data should return an error",
correctEntity,
`{}`,
correctEntity,
true,
license{},
},
{
"Signing JSON with incorrect private key should return an error",
incorrectEntity,
correctJSON,
correctEntity,
true,
license{},
},
{
"Verifying data with incorrect public key should return an error",
correctEntity,
correctJSON,
incorrectEntity,
true,
license{},
},
{
"Verifying data with correct public key should return correct data",
correctEntity,
correctJSON,
correctEntity,
false,
license{"user", 10, correctTime},
},
}

for _, tt := range tests {
t.Logf("Running: %s\n", tt.name)
buf := signAndWriteMessage(t, tt.signingEntity, tt.json)
e := license{}
publicKey := encodePublicKey(t, tt.verifyingEntity)
err = verifySignature(buf, publicKey, &e)
if tt.expectError {
require.Error(t, err)
continue
}

require.NoError(t, err)
require.Equal(t, tt.expectedOutput, e)
}
}
Loading