-
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 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 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
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,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/8vVu9vXbfiGkH6Cz32zBr1py2kW+n/D8XR5ZlbsV7P2ne3VXOv9 | ||
WTXSUFpkV1OrGY33j6Lg6OmcVhHTtCDDwCaB4iCHXDTVq9Yh2Er+ADIVtwARAQAB | ||
tDtEZ3JhcGggTGFicyAoRGdyYXBoIExhYnMgTGljZW5zaW5nIEtleSkgPGNvbnRh | ||
Y3RAZGdyYXBoLmlvPokCTgQTAQoAOBYhBA95WEve8LWjE9TFvnomeeH3SyppBQJd | ||
W0AMAhsDBQsJCAcCBhUKCQgLAg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Pnptrgg4S3ADx | ||
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") | ||
} | ||
|
||
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", "", "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) | ||
} | ||
} |
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