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 14 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. "+
"Restart Dgraph Zero with the appropriate license file.",
"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
70 changes: 70 additions & 0 deletions dgraph/cmd/zero/pgp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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/ioutil"
"os"

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

func enterpriseDetails(signedFile string, e *enterprise) error {
publicKeyFile, err := os.Open(Zero.Conf.GetString("public_key"))
if err != nil {
return errors.Wrapf(err, "while opening public key file")
}
defer publicKeyFile.Close()

entityList, err := openpgp.ReadArmoredKeyRing(publicKeyFile)
if err != nil {
return errors.Wrapf(err, "while reading public key")
}

sf, err := os.Open(signedFile)
Copy link

Choose a reason for hiding this comment

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

Do you want to close the sf file handle at some point or possibly defer sf.Close() after checking the error below?

if err != nil {
return errors.Wrapf(err, "while opening signed license file: %v", signedFile)
}

// The signed file is expected to be have ASCII encoding, so we have to decode it before
// reading.
b, err := armor.Decode(sf)
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")
}
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, e)
return errors.Wrapf(err, "while JSON unmarshaling body of license file")
}
54 changes: 53 additions & 1 deletion dgraph/cmd/zero/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,9 @@ func (n *node) applyProposal(e raftpb.Entry) (string, error) {
return p.Key, err
}
}
if p.Enterprise != nil {
state.Enterprise = p.Enterprise
}

if p.MaxLeaseId > state.MaxLeaseId {
state.MaxLeaseId = p.MaxLeaseId
Expand Down Expand Up @@ -502,11 +505,59 @@ func (n *node) initAndStartNode() error {
}()
}

if fpath := Zero.Conf.GetString("enterprise_license"); len(fpath) > 0 {
var e enterprise
if err := enterpriseDetails(fpath, &e); err != nil {
x.CheckfNoTrace(err)
}

proposal := &pb.ZeroProposal{
Enterprise: &pb.Enterprise{
Entity: e.Entity,
MaxNodes: e.MaxNodes,
ExpiryTs: e.Expiry.Unix(),
},
}

go func() {
for {
err := n.proposeAndWait(context.Background(), proposal)
Copy link

Choose a reason for hiding this comment

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

Just to double check, is the n.proposeAndWait safe to call on other threads/goroutines? I didn't see any documentation on the method whether it is or isn't but looks like it does some things like n.Proposals.Delete(key) within it that could potentially mutate or not be thread safe. I find it can be useful for future readers of your code to document which methods require locks and which don't since it looks like there are some locks being used within these methods.

if err == nil {
glog.Infof("Enterprise state proposed to the cluster")
break
}
if err == errInvalidProposal {
break
Copy link

Choose a reason for hiding this comment

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

Should anything be logged in this case or is it expected to no-op silently on an invalidProposal?

}
glog.Errorf("While proposing enterprise state: %v. Retrying...", err)
time.Sleep(3 * time.Second)
Copy link

Choose a reason for hiding this comment

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

Should there be any limit to the number of times this is tried or possibly some sort of exponential backoff? Or maybe worth a TODO for the future.

}
}()
}

go n.Run()
go n.BatchAndSendMessages()
return nil
}

// periodically checks the validity of the enterprise license and updates the membership state.
func (n *node) updateEnterpriseStatePeriodically(closer *y.Closer) {
defer closer.Done()

ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()

n.server.updateEnterpriseState()
for {
select {
case <-ticker.C:
n.server.updateEnterpriseState()
case <-closer.HasBeenClosed():
return
}
}
}

func (n *node) updateZeroMembershipPeriodically(closer *y.Closer) {
defer closer.Done()
ticker := time.NewTicker(10 * time.Second)
Expand Down Expand Up @@ -604,14 +655,15 @@ func (n *node) Run() {
// snapshot can cause select loop to block while deleting entries, so run
// it in goroutine
readStateCh := make(chan raft.ReadState, 100)
closer := y.NewCloser(4)
closer := y.NewCloser(5)
defer func() {
closer.SignalAndWait()
n.closer.Done()
glog.Infof("Zero Node.Run finished.")
}()

go n.snapshotPeriodically(closer)
go n.updateEnterpriseStatePeriodically(closer)
go n.updateZeroMembershipPeriodically(closer)
go n.checkQuorum(closer)
go n.RunReadIndexLoop(closer, readStateCh)
Expand Down
3 changes: 3 additions & 0 deletions dgraph/cmd/zero/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ instances to achieve high-availability.
// about the status of supporting annotation logs through the datadog exporter
flag.String("datadog.collector", "", "Send opencensus traces to Datadog. As of now, the trace"+
" exporter does not support annotation logs and would discard them.")
flag.String("enterprise_license", "", "Path to the enterprise license file")
// TODO - Only for testing, remove before shipping.
Copy link

Choose a reason for hiding this comment

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

Could consider upgrading this to a FIXME if it is supposed to be removed before shipping to prevent it sneaking by if FIXME caries a higher weight on your team.

flag.String("public_key", "", "Path to public key.")
}

func setupListener(addr string, port int, kind string) (listener net.Listener, err error) {
Expand Down
42 changes: 40 additions & 2 deletions dgraph/cmd/zero/zero.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ var (
errServerShutDown = errors.New("Server is being shut down")
)

type enterprise struct {
Entity string `json:"entity"`
MaxNodes uint64 `json:"max_nodes"`
Expiry time.Time `json:"expiry"`
}

// Server implements the zero server.
type Server struct {
x.SafeMutex
Expand Down Expand Up @@ -264,6 +270,26 @@ func (s *Server) updateZeroLeader() {
}
}

// updateEnterpriseState periodically checks the validity of the enterprise license
// based on its expiry.
func (s *Server) updateEnterpriseState() {
s.Lock()
defer s.Unlock()

// Return early if enterprise is not enabled. This would happen when user didn't supply us a
// license file yet.
if s.state.GetEnterprise() == nil {
return
}

expiry := time.Unix(s.state.Enterprise.ExpiryTs, 0)
if time.Now().Before(expiry) {
Copy link

Choose a reason for hiding this comment

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

looks like could potentially simplify this to one line since you already have a boolean expression:

s.state.Enterprise.Enabled = time.Now().Before(expiry)

s.state.Enterprise.Enabled = true
} else {
s.state.Enterprise.Enabled = false
}
}

func (s *Server) removeZero(nodeId uint64) {
s.Lock()
defer s.Unlock()
Expand Down Expand Up @@ -397,7 +423,7 @@ func (s *Server) removeNode(ctx context.Context, nodeId uint64, groupId uint32)
return s.Node.proposeAndWait(ctx, zp)
}

// Connect is used to connect the very first time with group zero.
// Connect is used by Alpha nodes to connect the very first time with group zero.
func (s *Server) Connect(ctx context.Context,
m *pb.Member) (resp *pb.ConnectionState, err error) {
// Ensures that connect requests are always serialized
Expand Down Expand Up @@ -435,10 +461,13 @@ func (s *Server) Connect(ctx context.Context,
}
}

numberOfNodes := len(ms.Zeros)
for _, group := range ms.Groups {
for _, member := range group.Members {
switch {
case member.Addr == m.Addr && m.Id == 0:
// TODO - Verify if we need the m.Id == 0 condition here and why.
// If we have this member, then we should just connect to it and return.
case member.Addr == m.Addr:
glog.Infof("Found a member with the same address. Returning: %+v", member)
conn.GetPools().Connect(m.Addr)
return &pb.ConnectionState{
Expand All @@ -460,9 +489,18 @@ func (s *Server) Connect(ctx context.Context,
" with same ID: %+v", member)
}
}
numberOfNodes++
}
}

// TODO - Zero MaxNodes should probably be an error.
maxNodes := s.state.GetEnterprise().GetMaxNodes()
if s.state.GetEnterprise().GetEnabled() && maxNodes != 0 &&
uint64(numberOfNodes) >= maxNodes {
return nil, errors.Errorf("ENTERPRISE_LIMIT_REACHED: You are already using the maximum "+
"number of nodes: [%v] permitted for your enterprise license.", maxNodes)
}

// Create a connection and check validity of the address by doing an Echo.
conn.GetPools().Connect(m.Addr)

Expand Down
9 changes: 9 additions & 0 deletions protos/pb.proto
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ message Group {
uint64 checksum = 4; // Stores a checksum.
}

message Enterprise {
string entity = 1;
uint64 maxNodes = 2;
int64 expiryTs = 3;
bool enabled = 4;
}

message ZeroProposal {
map<uint32, uint64> snapshot_ts = 1; // Group ID -> Snapshot Ts.
Member member = 2;
Expand All @@ -147,6 +154,7 @@ message ZeroProposal {
api.TxnContext txn = 7;
string key = 8; // Used as unique identifier for proposal id.
string cid = 9; // Used as unique identifier for the cluster.
Enterprise enterprise = 10;
}

// MembershipState is used to pack together the current membership state of all the nodes
Expand All @@ -161,6 +169,7 @@ message MembershipState {
uint64 maxRaftId = 6;
repeated Member removed = 7;
string cid = 8; // Used to uniquely identify the Dgraph cluster.
Enterprise enterprise = 9;
}

message ConnectionState {
Expand Down
Loading