-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
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 706746d
Add a EnterpriseEnabled field to MembershipState and propogate it to …
pawanrawal ce2b931
Dummy commit
pawanrawal 69871db
Remove useless print statements.
pawanrawal 5dd9a02
Check for max allowed nodes while calling Connect in Zero
pawanrawal c7bff37
Remove some TODOs which we don't need to worry about.
pawanrawal c880a3b
Verify signature from the file with PGP message containing data and s…
pawanrawal 9f0fe3e
Add another small comment.
pawanrawal 6bfb453
Propose enterprise state to the Zero cluster.
pawanrawal 6eda1a5
Read expiry from state in Zero
pawanrawal 80b9c11
Simplify code and consolidate Enterprise struct.
pawanrawal bbc98fc
Remove unnecessary locks.
pawanrawal 41fa1c6
Remove some TODOs
pawanrawal 2a4949d
Reduce ticker timer to 5sec.
pawanrawal 23ca13b
Address majority of the comments by pullrequests folks.
pawanrawal 08809dd
Add some tests for the enterprise details function.
pawanrawal 27ca7ee
Add a HTTP endpoint which allows proposing enterprise license.
pawanrawal de2beaf
Handle some TODOs
pawanrawal 8f06a59
Check EnterpriseEnabled flag in login handler.
pawanrawal 1f7d3f6
Check MaxNodes before applying enterprise proposal.
pawanrawal a2e5669
Remove enterprise_license flag as we have the hhtp endpoint for it.
pawanrawal 2c277d3
Check number of nodes before proposing enterprise license.
pawanrawal 277f10b
Rename enterprise to license and entity to user
pawanrawal 2fa8fb3
Rename e to l
pawanrawal a538d66
Refactor Connect function and also have public key as part of the code.
pawanrawal 5b223dc
Modify the error a bit.
pawanrawal fd1be0d
Remove enterprise_features flag from docker_compose files.
pawanrawal b5dcda7
Apply license in restartCluster
pawanrawal 85f85ca
Add some comments to the function as well.
pawanrawal c5bb124
Print out dummy_var
pawanrawal a053cd8
Remove DUMMY_VAR
pawanrawal 04b4641
Add the correct Dgraph public key. Tests would start failing now.
pawanrawal 101e1c0
Apply license valid for 30 days after proposing cid
pawanrawal 09969a0
Revert changes to functions.sh
pawanrawal 68064d6
Merge branch 'master' into pawan/license-file
pawanrawal ba7f3fb
Print info logs if license is about to expire in less than a week.
pawanrawal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/wrs1ajRiwbTehjgFi1S3BIvE5LdbNzbVAJP | ||
5p/eXgJfStI8vOTsvtsZDxskz2JbxmNBC3vfQqdmms8Vs764BVrtg77YLqCkhRsZ | ||
g47sBiRnffXBAoM3zR+gTAkl47hUP/GJXc/wXz9RWxa8jb1fHpUHqh4prQARAQAB | ||
tB9EZ3JhcGggTGFicyA8Y29udGFjdEBkZ3JhcGguaW8+iQJOBBMBCgA4FiEElb71 | ||
YXoTIYk1CVfE2dE672U82kUFAl1T7lcCGwMFCwkIBwIGFQoJCAsCBBYCAwECHgEC | ||
F4AACgkQ2dE672U82kWzLxAArQ50WlEy/8FE7X5I5pG6r1zM00kTHQDTfGP62/fF | ||
H4IisQnaZsCOA6jNqIKx67bqIVCAGg+8xPf2jUhgFc+KlNV9Fff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RZ8R6Rsm1jNIWeBwGKAc2y3o1vMaXJlmui2g== | ||
=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") | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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