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

Allow osnadmin CLI to connect without TLS #2106

Merged
merged 1 commit into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 27 additions & 17 deletions cmd/osnadmin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func executeForArgs(args []string) (output string, exit int, err error) {
//
app := kingpin.New("osnadmin", "Orderer Service Node (OSN) administration")
orderer := app.Flag("orderer-address", "Admin endpoint of the OSN").Short('o').Required().String()
caFile := app.Flag("ca-file", "Path to file containing PEM-encoded TLS CA certificate(s) for the OSN").Required().String()
clientCert := app.Flag("client-cert", "Path to file containing PEM-encoded X509 public key to use for mutual TLS communication with the OSN").Required().String()
clientKey := app.Flag("client-key", "Path to file containing PEM-encoded private key to use for mutual TLS communication with the OSN").Required().String()
caFile := app.Flag("ca-file", "Path to file containing PEM-encoded TLS CA certificate(s) for the OSN").String()
clientCert := app.Flag("client-cert", "Path to file containing PEM-encoded X509 public key to use for mutual TLS communication with the OSN").String()
clientKey := app.Flag("client-key", "Path to file containing PEM-encoded private key to use for mutual TLS communication with the OSN").String()

channel := app.Command("channel", "Channel actions")

Expand All @@ -63,21 +63,31 @@ func executeForArgs(args []string) (output string, exit int, err error) {
//
// flag validation
//
osnURL := fmt.Sprintf("https://%s", *orderer)

caCertPool := x509.NewCertPool()
caFilePEM, err := ioutil.ReadFile(*caFile)
if err != nil {
return "", 1, fmt.Errorf("reading orderer CA certificate: %s", err)
}
err = comm.AddPemToCertPool(caFilePEM, caCertPool)
if err != nil {
return "", 1, fmt.Errorf("adding ca-file PEM to cert pool: %s", err)
}
var (
osnURL string
caCertPool *x509.CertPool
tlsClientCert tls.Certificate
)
// TLS enabled
if *caFile != "" {
osnURL = fmt.Sprintf("https://%s", *orderer)
var err error
caCertPool = x509.NewCertPool()
caFilePEM, err := ioutil.ReadFile(*caFile)
if err != nil {
return "", 1, fmt.Errorf("reading orderer CA certificate: %s", err)
}
err = comm.AddPemToCertPool(caFilePEM, caCertPool)
if err != nil {
return "", 1, fmt.Errorf("adding ca-file PEM to cert pool: %s", err)
}

tlsClientCert, err := tls.LoadX509KeyPair(*clientCert, *clientKey)
if err != nil {
return "", 1, fmt.Errorf("loading client cert/key pair: %s", err)
tlsClientCert, err = tls.LoadX509KeyPair(*clientCert, *clientKey)
if err != nil {
return "", 1, fmt.Errorf("loading client cert/key pair: %s", err)
}
} else { // TLS disabled
osnURL = fmt.Sprintf("http://%s", *orderer)
}

var marshaledConfigBlock []byte
Expand Down
108 changes: 106 additions & 2 deletions cmd/osnadmin/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,12 @@ var _ = Describe("osnadmin", func() {
})

JustBeforeEach(func() {
testServer.TLS = tlsConfig
testServer.StartTLS()
if tlsConfig != nil {
testServer.TLS = tlsConfig
testServer.StartTLS()
} else {
testServer.Start()
}

u, err := url.Parse(testServer.URL)
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -196,6 +200,62 @@ var _ = Describe("osnadmin", func() {
checkOutput(output, exit, err, 404, expectedOutput)
})
})

Context("when TLS is disabled", func() {
BeforeEach(func() {
tlsConfig = nil
})

It("uses the channel participation API to list all channels", func() {
args := []string{
"channel",
"list",
"--orderer-address", ordererURL,
}
output, exit, err := executeForArgs(args)
Expect(err).NotTo(HaveOccurred())
Expect(exit).To(Equal(0))

expectedOutput := types.ChannelList{
Channels: []types.ChannelInfoShort{
{
Name: "participation-trophy",
URL: "/participation/v1/channels/participation-trophy",
},
{
Name: "another-participation-trophy",
URL: "/participation/v1/channels/another-participation-trophy",
},
},
SystemChannel: &types.ChannelInfoShort{
Name: "fight-the-system",
URL: "/participation/v1/channels/fight-the-system",
},
}
checkOutput(output, exit, err, 200, expectedOutput)
})

It("uses the channel participation API to list the details of a single channel", func() {
args := []string{
"channel",
"list",
"--orderer-address", ordererURL,
"--channel-id", "tell-me-your-secrets",
}
output, exit, err := executeForArgs(args)
Expect(err).NotTo(HaveOccurred())
Expect(exit).To(Equal(0))

expectedOutput := types.ChannelInfo{
Name: "asparagus",
URL: "/participation/v1/channels/asparagus",
ConsensusRelation: "broccoli",
Status: "carrot",
Height: 987,
}
checkOutput(output, exit, err, 200, expectedOutput)
})
})
})

Describe("Remove", func() {
Expand Down Expand Up @@ -237,6 +297,25 @@ var _ = Describe("osnadmin", func() {
checkOutput(output, exit, err, 404, expectedOutput)
})
})

Context("when TLS is disabled", func() {
BeforeEach(func() {
tlsConfig = nil
})

It("uses the channel participation API to remove a channel", func() {
args := []string{
"channel",
"remove",
"--orderer-address", ordererURL,
"--channel-id", channelID,
}
output, exit, err := executeForArgs(args)
Expect(err).NotTo(HaveOccurred())
Expect(exit).To(Equal(0))
Expect(output).To(Equal("Status: 204\n"))
})
})
})

Describe("Join", func() {
Expand Down Expand Up @@ -391,6 +470,31 @@ var _ = Describe("osnadmin", func() {
checkOutput(output, exit, err, 405, expectedOutput)
})
})

Context("when TLS is disabled", func() {
BeforeEach(func() {
tlsConfig = nil
})

It("uses the channel participation API to join a channel", func() {
args := []string{
"channel",
"join",
"--orderer-address", ordererURL,
"--channel-id", channelID,
"--config-block", blockPath,
}
output, exit, err := executeForArgs(args)
expectedOutput := types.ChannelInfo{
Name: "apple",
URL: "/participation/v1/channels/apple",
ConsensusRelation: "banana",
Status: "orange",
Height: 123,
}
checkOutput(output, exit, err, 201, expectedOutput)
})
})
})

Describe("Flags", func() {
Expand Down