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

Update gRPC #167

Merged
merged 7 commits into from
Oct 18, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: ['1.15', '1.16', '1.17']
go: ['1.19', '1.20']
steps:
- name: Updating ...
run: sudo apt-get -qq update
Expand Down
7 changes: 3 additions & 4 deletions canonical_facts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -154,7 +153,7 @@ func GetCanonicalFacts() (*CanonicalFacts, error) {
// readFile reads the contents of filename into a string, trims whitespace,
// and returns the result.
func readFile(filename string) (string, error) {
data, err := ioutil.ReadFile(filename)
data, err := os.ReadFile(filename)
if err != nil {
return "", err
}
Expand All @@ -167,7 +166,7 @@ func readCert(filename string) (string, error) {
var asn1Data []byte
switch filepath.Ext(filename) {
case ".pem":
data, err := ioutil.ReadFile(filename)
data, err := os.ReadFile(filename)
if err != nil {
return "", err
}
Expand All @@ -179,7 +178,7 @@ func readCert(filename string) (string, error) {
asn1Data = append(asn1Data, block.Bytes...)
default:
var err error
asn1Data, err = ioutil.ReadFile(filename)
asn1Data, err = os.ReadFile(filename)
if err != nil {
return "", err
}
Expand Down
4 changes: 3 additions & 1 deletion canonical_facts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ func TestCanonicalFactsUnmarshalJSON(t *testing.T) {
},
{
description: "valid",
input: []byte(`{"insights_id":"bb69cd34-263f-444c-9278-5935b61d7f60","machine_id":"acc046d0-0add-4550-ac7c-5a833b1b6470","bios_uuid":"d8ec3cd5-a6bc-4742-bd2f-32940da182b0","subscription_manager_id":"bc452b83-c4ee-4b80-91d8-98ff816b2440","ip_addresses":["1.2.3.4", "5.6.7.8"],"fqdn":"foo.bar.com","mac_addresses":["CC:D1:7A:44:6D:1B", "A7:03:90:D0:05:A7"]}`),
input: []byte(
`{"insights_id":"bb69cd34-263f-444c-9278-5935b61d7f60","machine_id":"acc046d0-0add-4550-ac7c-5a833b1b6470","bios_uuid":"d8ec3cd5-a6bc-4742-bd2f-32940da182b0","subscription_manager_id":"bc452b83-c4ee-4b80-91d8-98ff816b2440","ip_addresses":["1.2.3.4", "5.6.7.8"],"fqdn":"foo.bar.com","mac_addresses":["CC:D1:7A:44:6D:1B", "A7:03:90:D0:05:A7"]}`,
),
want: CanonicalFacts{
InsightsID: "bb69cd34-263f-444c-9278-5935b61d7f60",
MachineID: "acc046d0-0add-4550-ac7c-5a833b1b6470",
Expand Down
6 changes: 5 additions & 1 deletion cmd/ygg/activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ func activate() error {
}
activeState := properties["ActiveState"]
if activeState.(string) != "active" {
return fmt.Errorf("error: The unit %v failed to start. Run 'systemctl status %v' for more information", unitName, unitName)
return fmt.Errorf(
"error: The unit %v failed to start. Run 'systemctl status %v' for more information",
unitName,
unitName,
)
}

return nil
Expand Down
75 changes: 53 additions & 22 deletions cmd/ygg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/redhatinsights/yggdrasil"
internal "github.com/redhatinsights/yggdrasil/internal"
"github.com/urfave/cli/v2"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/term"
)

const successPrefix = "\033[32m●\033[0m"
Expand Down Expand Up @@ -84,16 +84,25 @@ func main() {
Usage: "register against `URL`",
},
},
Usage: "Connects the system to " + yggdrasil.Provider,
UsageText: fmt.Sprintf("%v connect [command options]", app.Name),
Description: fmt.Sprintf("The connect command connects the system to Red Hat Subscription Management and %v and activates the %v daemon that enables %v to interact with the system. For details visit: https://red.ht/connector", yggdrasil.Provider, yggdrasil.BrandName, yggdrasil.Provider),
Usage: "Connects the system to " + yggdrasil.Provider,
UsageText: fmt.Sprintf("%v connect [command options]", app.Name),
Description: fmt.Sprintf(
"The connect command connects the system to Red Hat Subscription Management and %v and activates the %v daemon that enables %v to interact with the system. For details visit: https://red.ht/connector",
yggdrasil.Provider,
yggdrasil.BrandName,
yggdrasil.Provider,
),
Action: func(c *cli.Context) error {
hostname, err := os.Hostname()
if err != nil {
return cli.Exit(err, 1)
}

fmt.Printf("Connecting %v to %v.\nThis might take a few seconds.\n\n", hostname, yggdrasil.Provider)
fmt.Printf(
"Connecting %v to %v.\nThis might take a few seconds.\n\n",
hostname,
yggdrasil.Provider,
)

uuid, err := getConsumerUUID()
if err != nil {
Expand All @@ -114,7 +123,7 @@ func main() {
}
if password == "" {
fmt.Print("Password: ")
data, err := terminal.ReadPassword(int(os.Stdin.Fd()))
data, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return cli.Exit(err, 1)
}
Expand All @@ -128,7 +137,11 @@ func main() {
s.Start()
var err error
if c.String("organization") != "" {
err = registerActivationKey(c.String("organization"), c.StringSlice("activation-key"), c.String("server"))
err = registerActivationKey(
c.String("organization"),
c.StringSlice("activation-key"),
c.String("server"),
)
} else {
err = registerPassword(username, password, c.String("server"))
}
Expand Down Expand Up @@ -158,16 +171,25 @@ func main() {
},
},
{
Name: "disconnect",
Usage: "Disconnects the system from " + yggdrasil.Provider,
UsageText: fmt.Sprintf("%v disconnect", app.Name),
Description: fmt.Sprintf("The disconnect command disconnects the system from Red Hat Subscription Management and %v and deactivates the %v daemon. %v will no longer be able to interact with the system.", yggdrasil.Provider, yggdrasil.BrandName, yggdrasil.Provider),
Name: "disconnect",
Usage: "Disconnects the system from " + yggdrasil.Provider,
UsageText: fmt.Sprintf("%v disconnect", app.Name),
Description: fmt.Sprintf(
"The disconnect command disconnects the system from Red Hat Subscription Management and %v and deactivates the %v daemon. %v will no longer be able to interact with the system.",
yggdrasil.Provider,
yggdrasil.BrandName,
yggdrasil.Provider,
),
Action: func(c *cli.Context) error {
hostname, err := os.Hostname()
if err != nil {
return cli.Exit(err, 1)
}
fmt.Printf("Disconnecting %v from %v.\nThis might take a few seconds.\n\n", hostname, yggdrasil.Provider)
fmt.Printf(
"Disconnecting %v from %v.\nThis might take a few seconds.\n\n",
hostname,
yggdrasil.Provider,
)

s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Suffix = fmt.Sprintf(" Deactivating the %v daemon", yggdrasil.BrandName)
Expand All @@ -192,11 +214,14 @@ func main() {
},
},
{
Name: "canonical-facts",
Hidden: true,
Usage: "Prints canonical facts about the system.",
UsageText: fmt.Sprintf("%v canonical-facts", app.Name),
Description: fmt.Sprintf("The canonical-facts command prints data that uniquely identifies the system in the %v inventory service. Use only as directed for debugging purposes.", yggdrasil.Provider),
Name: "canonical-facts",
Hidden: true,
Usage: "Prints canonical facts about the system.",
UsageText: fmt.Sprintf("%v canonical-facts", app.Name),
Description: fmt.Sprintf(
"The canonical-facts command prints data that uniquely identifies the system in the %v inventory service. Use only as directed for debugging purposes.",
yggdrasil.Provider,
),
Action: func(c *cli.Context) error {
facts, err := yggdrasil.GetCanonicalFacts()
if err != nil {
Expand Down Expand Up @@ -247,16 +272,22 @@ func main() {
}
w.Flush()
default:
return cli.Exit(fmt.Errorf("unsupported value for '--format': %v", c.String("format")), 1)
return cli.Exit(
fmt.Errorf("unsupported value for '--format': %v", c.String("format")),
1,
)
}
return nil
},
},
{
Name: "status",
Usage: "Prints status of the system's connection to " + yggdrasil.Provider,
UsageText: fmt.Sprintf("%v status", app.Name),
Description: fmt.Sprintf("The status command prints the state of the connection to Red Hat Subscription Management and %v.", yggdrasil.Provider),
Name: "status",
Usage: "Prints status of the system's connection to " + yggdrasil.Provider,
UsageText: fmt.Sprintf("%v status", app.Name),
Description: fmt.Sprintf(
"The status command prints the state of the connection to Red Hat Subscription Management and %v.",
yggdrasil.Provider,
),
Action: func(c *cli.Context) error {
hostname, err := os.Hostname()
if err != nil {
Expand Down
15 changes: 10 additions & 5 deletions cmd/yggd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -100,7 +99,7 @@ func startProcess(file string, env []string, delay time.Duration, died chan int)
return
}

if err := ioutil.WriteFile(filepath.Join(pidDirPath, filepath.Base(file)+".pid"), []byte(fmt.Sprintf("%v", cmd.Process.Pid)), 0644); err != nil {
if err := os.WriteFile(filepath.Join(pidDirPath, filepath.Base(file)+".pid"), []byte(fmt.Sprintf("%v", cmd.Process.Pid)), 0644); err != nil {
log.Errorf("cannot write to file: %v", err)
return
}
Expand Down Expand Up @@ -142,7 +141,7 @@ func killProcess(pid int) error {
}

func killWorker(pidFile string) error {
data, err := ioutil.ReadFile(pidFile)
data, err := os.ReadFile(pidFile)
if err != nil {
return fmt.Errorf("cannot read contents of file: %w", err)
}
Expand All @@ -166,7 +165,7 @@ func killWorkers() error {
if err := os.MkdirAll(pidDirPath, 0755); err != nil {
return fmt.Errorf("cannot create directory: %w", err)
}
fileInfos, err := ioutil.ReadDir(pidDirPath)
fileInfos, err := os.ReadDir(pidDirPath)
if err != nil {
return fmt.Errorf("cannot read contents of directory: %w", err)
}
Expand Down Expand Up @@ -203,7 +202,13 @@ func watchWorkerDir(dir string, env []string, died chan int) {
}
case notify.InDelete, notify.InMovedFrom:
workerName := filepath.Base(e.Path())
pidFilePath := filepath.Join(yggdrasil.LocalstateDir, "run", yggdrasil.LongName, "workers", workerName+".pid")
pidFilePath := filepath.Join(
yggdrasil.LocalstateDir,
"run",
yggdrasil.LongName,
"workers",
workerName+".pid",
)

if err := killWorker(pidFilePath); err != nil {
log.Errorf("cannot kill worker: %v", err)
Expand Down
11 changes: 9 additions & 2 deletions cmd/yggd/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/redhatinsights/yggdrasil"
pb "github.com/redhatinsights/yggdrasil/protocol"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

type worker struct {
Expand Down Expand Up @@ -44,7 +45,10 @@ func newDispatcher() *dispatcher {
}
}

func (d *dispatcher) Register(ctx context.Context, r *pb.RegistrationRequest) (*pb.RegistrationResponse, error) {
func (d *dispatcher) Register(
ctx context.Context,
r *pb.RegistrationRequest,
) (*pb.RegistrationResponse, error) {
d.RLock()
if _, prs := d.workers[r.GetHandler()]; prs {
d.RUnlock()
Expand Down Expand Up @@ -144,7 +148,10 @@ func (d *dispatcher) sendData() {
data.Content = content
}

conn, err := grpc.Dial("unix:"+w.addr, grpc.WithInsecure())
conn, err := grpc.Dial(
"unix:"+w.addr,
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
log.Errorf("cannot dial socket: %v", err)
return
Expand Down
16 changes: 11 additions & 5 deletions cmd/yggd/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"crypto/tls"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"

Expand Down Expand Up @@ -44,14 +44,17 @@ func get(url string) ([]byte, error) {
}
defer resp.Body.Close()

data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("cannot read response body: %w", err)
}
log.Debugf("received HTTP %v", resp.Status)

if resp.StatusCode >= 400 {
return nil, &yggdrasil.APIResponseError{Code: resp.StatusCode, Body: strings.TrimSpace(string(data))}
return nil, &yggdrasil.APIResponseError{
Code: resp.StatusCode,
Body: strings.TrimSpace(string(data)),
}
}

return data, nil
Expand All @@ -77,14 +80,17 @@ func post(url string, headers map[string]string, body []byte) error {
}
defer resp.Body.Close()

data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("cannot read response body: %w", err)
}
log.Debugf("received HTTP %v", resp.Status)

if resp.StatusCode >= 400 {
return &yggdrasil.APIResponseError{Code: resp.StatusCode, Body: strings.TrimSpace(string(data))}
return &yggdrasil.APIResponseError{
Code: resp.StatusCode,
Body: strings.TrimSpace(string(data)),
}
}

return nil
Expand Down
9 changes: 4 additions & 5 deletions cmd/yggd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"crypto/sha256"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"os"
"os/signal"
Expand Down Expand Up @@ -194,17 +193,17 @@ func main() {
}

// Read certificates, create a TLS config, and initialize HTTP client
certData, err := ioutil.ReadFile(c.String("cert-file"))
certData, err := os.ReadFile(c.String("cert-file"))
if err != nil {
return cli.Exit(fmt.Errorf("cannot read certificate file: %v", err), 1)
}
keyData, err := ioutil.ReadFile(c.String("key-file"))
keyData, err := os.ReadFile(c.String("key-file"))
if err != nil {
return cli.Exit(fmt.Errorf("cannot read key file: %w", err), 1)
}
rootCAs := make([][]byte, 0)
for _, file := range c.StringSlice("ca-root") {
data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
if err != nil {
return cli.Exit(fmt.Errorf("cannot read certificate authority: %v", err), 1)
}
Expand Down Expand Up @@ -369,7 +368,7 @@ func main() {
return cli.Exit(fmt.Errorf("cannot create directory: %w", err), 1)
}

fileInfos, err := ioutil.ReadDir(workerPath)
fileInfos, err := os.ReadDir(workerPath)
if err != nil {
return cli.Exit(fmt.Errorf("cannot read contents of directory: %w", err), 1)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/yggd/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"strconv"
Expand All @@ -27,7 +26,7 @@ func (e *errorTag) Is(o error) bool {
// readTags reads from its input, unmarshalling the TOML-encoded value to a map.
// It then parses the map values into a map of string values.
func readTags(in io.Reader) (map[string]string, error) {
data, err := ioutil.ReadAll(in)
data, err := io.ReadAll(in)
if err != nil {
return nil, fmt.Errorf("cannot read input: %w", err)
}
Expand Down
Loading
Loading