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 25 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
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 a flag or 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
125 changes: 125 additions & 0 deletions dgraph/cmd/zero/pgp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* 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-----

mQINBF1T7lcBEADmxqS4gowV74h92QVehIchRkkZ74uQgFDQUPhs89vf5EvCci8m
LJYrsrBrDBQESjw+jQVQumtSnXqUIcVdumG80Nkgg01JuANOtqRs0vfd2P4piqxp
VCGrBATl0+9PrlQn/e8x6V449HLUeAx8jxML/us08RFzbLYoIFEnBdwbIJDfW6tl
CPS02Nj3ZW83crRCwyNjkSrSNt/TW5hVdYYveBe4urtJJgd/HWoytwHygtDIz5oc
ZjWMQxPXs2e0pYCTSCeqOOUu1LR7K6sHrhgYu1xMq3F8Vr9oqH5ZnwwydYd4GWQg
VfZEb/suXc9raxCcvIU5R32IHrfbFL5NQB9Cv9LxS4/13eoaIQ77eNJb0HYzo2CB
z88pidYGFuRzb6nkvi641XsBux0ox+4jXZJW6n83hsTc2Gjbo1JQZXU+3PbcMj8i
iWTwoDtev9uIFFWsSCGLulQVYH+0cdiBvmR0Jfb5JkE7d69+F7//AVD5Zcs6fnn3
kKNomPF4v9ujQEBh7SGSZCxDHwGL/wrs1aj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lFAugBSOgujb5whG+
hEaQQos8sHDOEka0KbCkTurYHt5+0enwFMCCbWQIT5z+j46yyAO9Om9p+M4H9qV7
INbe2kxJPareejuWnf9Xy2FVzgO+7YGZmxiSrHurRBoZO8KNvjib23mUXVJOKMVB
6gIZciMNVJ3HYTajtExS94eguukL9g+mAMSgRttNM6xlkJRCnzx55ie1VQ4kYhVR
wbAzcSLeovdXC7FaCKtBRgsY/kDmnxJ6qlWzcbOJQ4FlRnHtGQSqFBPUUQVJuuMB
zaY83TVDscYMz42G7eZ2skRZ8R6Rsm1jNIWeBwGKAc2y3o1vMaXJlmui2g==
=9fdK
-----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", "", "[email protected]", &packet.Config{
RSABits: 4096,
DefaultHash: crypto.SHA512,
})

require.NoError(t, err)
incorrectEntity, err := openpgp.NewEntity("incorrect", "", "[email protected]", &packet.Config{
RSABits: 4096,
DefaultHash: crypto.SHA512,
})
require.NoError(t, err)
correctJSON := `{"entity": "entity", "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{"entity", 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