Skip to content

Commit

Permalink
Minor MSP-related cleanup
Browse files Browse the repository at this point in the history
- Remove os.Stdout output in test
- Change revoke time to UTC
- Add constant for YEAR
- Update add/revoke tests to avoid races

FAB-17546

Signed-off-by: Will Lahti <wtlahti@us.ibm.com>
  • Loading branch information
wlahti authored and sykesm committed Mar 19, 2020
1 parent 1d92fca commit ac367be
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
7 changes: 5 additions & 2 deletions pkg/config/msp.go
Expand Up @@ -624,6 +624,9 @@ func getOrgMSPValue(org *cb.ConfigGroup) (*cb.ConfigValue, error) {
return configValue, nil
}

// YEAR is a time duration for a standard 365 day year.
const YEAR = 365 * 24 * time.Hour

// RevokeCertificateFromMSP takes a variadic list of x509 certificates, creates
// a new CRL signed by the specified ca certificate and private key, and appends
// it to the revocation list for the specified application org MSP.
Expand All @@ -640,7 +643,7 @@ func RevokeCertificateFromMSP(config *cb.Config, orgName string, caCert *x509.Ce

// TODO validate that this certificate was issued by this MSP

revokeTime := time.Now()
revokeTime := time.Now().UTC()
revokedCertificates := make([]pkix.RevokedCertificate, len(certs))
for i, cert := range certs {
revokedCertificates[i] = pkix.RevokedCertificate{
Expand All @@ -649,7 +652,7 @@ func RevokeCertificateFromMSP(config *cb.Config, orgName string, caCert *x509.Ce
}
}

crlBytes, err := caCert.CreateCRL(rand.Reader, caPrivKey, revokedCertificates, revokeTime, revokeTime.Add(365*24*time.Hour))
crlBytes, err := caCert.CreateCRL(rand.Reader, caPrivKey, revokedCertificates, revokeTime, revokeTime.Add(YEAR))
if err != nil {
return err
}
Expand Down
48 changes: 20 additions & 28 deletions pkg/config/msp_test.go
Expand Up @@ -14,7 +14,6 @@ import (
"encoding/base64"
"encoding/pem"
"fmt"
"os"
"testing"

"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -869,13 +868,6 @@ func TestAddRootCAToMSP(t *testing.T) {

func TestAddRootCAToMSPFailure(t *testing.T) {
t.Parallel()
gt := NewGomegaWithT(t)

channelGroup, err := baseApplicationChannelGroup()
gt.Expect(err).ToNot(HaveOccurred())
config := &cb.Config{
ChannelGroup: channelGroup,
}

tests := []struct {
spec string
Expand Down Expand Up @@ -904,6 +896,13 @@ func TestAddRootCAToMSPFailure(t *testing.T) {
t.Run(tc.spec, func(t *testing.T) {
t.Parallel()
gt := NewGomegaWithT(t)

channelGroup, err := baseApplicationChannelGroup()
gt.Expect(err).ToNot(HaveOccurred())
config := &cb.Config{
ChannelGroup: channelGroup,
}

err = AddRootCAToMSP(config, tc.cert, "Org1")
gt.Expect(err).To(MatchError(tc.expectedErr))
})
Expand All @@ -929,8 +928,6 @@ func TestRevokeCertificateFromMSP(t *testing.T) {
err = RevokeCertificateFromMSP(config, "Org1", caCert, caPrivKey, cert)
gt.Expect(err).ToNot(HaveOccurred())

protolator.DeepMarshalJSON(os.Stdout, config)

org1MSP, err = GetMSPConfigurationForApplicationOrg(config, "Org1")
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(org1MSP.RevocationList).To(HaveLen(2))
Expand Down Expand Up @@ -1284,29 +1281,18 @@ func TestRevokeCertificateFromMSP(t *testing.T) {

func TestRevokeCertificateFromMSPFailure(t *testing.T) {
t.Parallel()
gt := NewGomegaWithT(t)

channelGroup, err := baseApplicationChannelGroup()
gt.Expect(err).ToNot(HaveOccurred())
config := &cb.Config{
ChannelGroup: channelGroup,
}

caCert, caPrivKey := generateCACertAndPrivateKey("org1.example.com")
cert, _ := generateCertAndPrivateKeyFromCACert("Org1", caCert, caPrivKey)

tests := []struct {
spec string
orgName string
configModFunc func() *cb.Config
expectedErr string
spec string
orgName string
expectedErr string
}{
{
spec: "org not defined in config",
orgName: "not-an-org",
configModFunc: func() *cb.Config {
return config
},
spec: "org not defined in config",
orgName: "not-an-org",
expectedErr: "application org with name 'not-an-org' not found",
},
}
Expand All @@ -1316,8 +1302,14 @@ func TestRevokeCertificateFromMSPFailure(t *testing.T) {
t.Run(tc.spec, func(t *testing.T) {
t.Parallel()
gt := NewGomegaWithT(t)
config := tc.configModFunc()
err := RevokeCertificateFromMSP(config, tc.orgName, caCert, caPrivKey, cert)

channelGroup, err := baseApplicationChannelGroup()
gt.Expect(err).ToNot(HaveOccurred())
config := &cb.Config{
ChannelGroup: channelGroup,
}

err = RevokeCertificateFromMSP(config, tc.orgName, caCert, caPrivKey, cert)
gt.Expect(err).To(MatchError(tc.expectedErr))
})
}
Expand Down

0 comments on commit ac367be

Please sign in to comment.