当前位置:网站首页>Goframe framework (RK boot): enable tls/ssl

Goframe framework (RK boot): enable tls/ssl

2022-06-23 03:09:00 Trespass

Introduce

Through a complete example , stay gogf/gf Open in frame TLS/SSL, That's what we often say https.

We will use rk-boot To start up gogf/gf Microservices .

Please visit the following address for a complete tutorial :

Generate Self-Signed Certificate

Users can purchase certificates from major cloud manufacturers , Or use cfssl Create a custom certificate .

Let's introduce how to generate a certificate locally .

1. download cfssl & cfssljson Command line

Recommended rk Command line to download .

$ go get -u github.com/rookie-ninja/rk/cmd/rk
$ rk install cfssl
$ rk install cfssljson

Download from the official website

$ go get github.com/cloudflare/cfssl/cmd/cfssl
$ go get github.com/cloudflare/cfssl/cmd/cfssljson

2. Generate CA

$ cfssl print-defaults config > ca-config.json
$ cfssl print-defaults csr > ca-csr.json

Modify... As needed ca-config.json and ca-csr.json.

$ cfssl gencert -initca ca-csr.json | cfssljson -bare ca -

3. Generate server certificate

server.csr,server.pem and server-key.pem Will be generated .

$ cfssl gencert -config ca-config.json -ca ca.pem -ca-key ca-key.pem -profile www ca-csr.json | cfssljson -bare server

install

go get github.com/rookie-ninja/rk-boot/gf

Quick start

rk-boot Support to make gogf/gf Service get certificate .

  • Local file system
  • Remote file system
  • Consul
  • ETCD

Let's first look at how to get a certificate locally and start .

1. establish boot.yaml

In this case , We only start the certificate of the server . among ,locale Used to distinguish between different environments cert.

Please refer to the previous article for details :

---
cert:
  - name: "local-cert"                     # Required
    provider: "localFs"                    # Required, etcd, consul, localFs, remoteFs are supported options
    locale: "*::*::*::*"                   # Required, default: ""
    serverCertPath: "cert/server.pem"      # Optional, default: "", path of certificate on local FS
    serverKeyPath: "cert/server-key.pem"   # Optional, default: "", path of certificate on local FS
gf:
  - name: greeter
    port: 8080
    enabled: true
    enableReflection: true
    cert:
      ref: "local-cert"                    # Enable grpc TLS

2. establish main.go

// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.

package main

import (
	"context"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/rookie-ninja/rk-boot"
	"github.com/rookie-ninja/rk-boot/gf"
	"net/http"
)

// @title Swagger Example API
// @version 1.0
// @description This is a sample rk-demo server.
// @termsOfService http://swagger.io/terms/

// @securityDefinitions.basic BasicAuth

// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email [email protected]

// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
func main() {
	// Create a new boot instance.
	boot := rkboot.NewBoot()

	// Register handler
	entry := rkbootgf.GetGfEntry("greeter")
	entry.Server.BindHandler("/v1/hello", hello)

	// Bootstrap
	boot.Bootstrap(context.TODO())

	boot.WaitForShutdownSig(context.TODO())
}

// @Summary Hello
// @Id 1
// @Tags Hello
// @version 1.0
// @produce application/json
// @Success 200 string string
// @Router /v1/hello [get]
func hello(ctx *ghttp.Request) {
	ctx.Response.WriteHeader(http.StatusOK)
	ctx.Response.WriteJson(map[string]string{
		"message": "hello!",
	})
}

3. Folder structure

.
├── boot.yaml
├── cert
│   ├── server-key.pem
│   └── server.pem
├── go.mod
├── go.sum
└── main.go

1 directory, 6 files

4. start-up main.go

$ go run main.go

5. verification

$ curl -X GET --insecure https://localhost:8080/v1/hello     
{"message":"hello!"}

framework

Parameter Introduction

1. Read certificate locally

Configuration item

details

need

The default value is

cert.localFs.name

Local file system getter name

yes

""

cert.localFs.locale

follow locale: \<realm>::\<region>::\<az>::\<domain>

yes

""

cert.localFs.serverCertPath

Server certificate path

no

""

cert.localFs.serverKeyPath

Server certificate key path

no

""

cert.localFs.clientCertPath

Client certificate path

no

""

cert.localFs.clientCertPath

Client certificate key path

no

""

  • Example
---
cert:
  - name: "local-cert"                     # Required
    description: "Description of entry"    # Optional
    provider: "localFs"                    # Required, etcd, consul, localFs, remoteFs are supported options
    locale: "*::*::*::*"                   # Required, default: ""
    serverCertPath: "cert/server.pem"      # Optional, default: "", path of certificate on local FS
    serverKeyPath: "cert/server-key.pem"   # Optional, default: "", path of certificate on local FS
gf:
  - name: greeter
    port: 8080
    enabled: true
    enableReflection: true
    cert:
      ref: "local-cert"                    # Enable grpc TLS

2. Read certificate from remote file service

Configuration item

details

need

The default value is

cert.remoteFs.name

Remote file service getter name

yes

""

cert.remoteFs.locale

follow locale:\<realm>::\<region>::\<az>::\<domain>

yes

""

cert.remoteFs.endpoint

Remote address : http://x.x.x.x perhaps x.x.x.x

yes

N/A

cert.remoteFs.basicAuth

Basic auth: <user:pass>.

no

""

cert.remoteFs.serverCertPath

Server certificate path

no

""

cert.remoteFs.serverKeyPath

Server certificate key path

no

""

cert.remoteFs.clientCertPath

Client certificate path

no

""

cert.remoteFs.clientCertPath

Client certificate key path

no

""

  • Example
---
cert:
  - name: "remote-cert"                    # Required
    description: "Description of entry"    # Optional
    provider: "remoteFs"                   # Required, etcd, consul, localFs, remoteFs are supported options
    endpoint: "localhost:8081"             # Required, both http://x.x.x.x or x.x.x.x are acceptable
    locale: "*::*::*::*"                   # Required, default: ""
    serverCertPath: "cert/server.pem"      # Optional, default: "", path of certificate on local FS
    serverKeyPath: "cert/server-key.pem"   # Optional, default: "", path of certificate on local FS
gf:
  - name: greeter
    port: 8080
    enabled: true
    cert:
      ref: "remote-cert"                   # Enable grpc TLS

3. from Consul Read the certificate

Configuration item

details

need

The default value is

cert.consul.name

Consul Getter name

yes

""

cert.consul.locale

follow locale: \<realm>::\<region>::\<az>::\<domain>

yes

""

cert.consul.endpoint

Consul Address : http://x.x.x.x or x.x.x.x

yes

N/A

cert.consul.datacenter

Consul Data Center

yes

""

cert.consul.token

Consul Access key

no

""

cert.consul.basicAuth

Consul Basic auth, Format :<user:pass>.

no

""

cert.consul.serverCertPath

Server certificate path

no

""

cert.consul.serverKeyPath

Server certificate key path

no

""

cert.consul.clientCertPath

Server certificate key path

no

""

cert.consul.clientCertPath

Server certificate key path

no

""

  • Example
---
cert:
  - name: "consul-cert"                    # Required
    provider: "consul"                     # Required, etcd, consul, localFS, remoteFs are supported options
    description: "Description of entry"    # Optional
    locale: "*::*::*::*"                   # Required, ""
    endpoint: "localhost:8500"             # Required, http://x.x.x.x or x.x.x.x both acceptable.
    datacenter: "dc1"                      # Optional, default: "", consul datacenter
    serverCertPath: "server.pem"           # Optional, default: "", key of value in consul
    serverKeyPath: "server-key.pem"        # Optional, default: "", key of value in consul
gf:
  - name: greeter
    port: 8080
    enabled: true
    cert:
      ref: "consul-cert"                   # Enable grpc TLS

4. from ETCD Read the certificate

Configuration item

details

need

The default value is

cert.etcd.name

ETCD Getter name

yes

""

cert.etcd.locale

follow locale: \<realm>::\<region>::\<az>::\<domain>

yes

""

cert.etcd.endpoint

ETCD Address :http://x.x.x.x or x.x.x.x

yes

N/A

cert.etcd.basicAuth

ETCD basic auth, Format :<user:pass>.

no

""

cert.etcd.serverCertPath

Server certificate path

no

""

cert.etcd.serverKeyPath

Server certificate path

no

""

cert.etcd.clientCertPath

Client certificate path

no

""

cert.etcd.clientCertPath

Client certificate key path

no

""

  • Example
---
cert:
  - name: "etcd-cert"                      # Required
    description: "Description of entry"    # Optional
    provider: "etcd"                       # Required, etcd, consul, localFs, remoteFs are supported options
    locale: "*::*::*::*"                   # Required, default: ""
    endpoint: "localhost:2379"             # Required, http://x.x.x.x or x.x.x.x both acceptable.
    serverCertPath: "server.pem"           # Optional, default: "", key of value in etcd
    serverKeyPath: "server-key.pem"        # Optional, default: "", key of value in etcd
gf:
  - name: greeter
    port: 8080
    enabled: true
    cert:
      ref: "etcd-cert"                   # Enable grpc TLS
原网站

版权声明
本文为[Trespass ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/01/202201222257150212.html