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

VersionTLS constants and VersionName(version uint16) are missing #4717

Open
mohrobati opened this issue Jan 24, 2025 · 0 comments · May be fixed by #4718
Open

VersionTLS constants and VersionName(version uint16) are missing #4717

mohrobati opened this issue Jan 24, 2025 · 0 comments · May be fixed by #4718

Comments

@mohrobati
Copy link

Problem

Let's say I plan to use a go module like https://github.com/redis/go-redis in my go project and compile it with TinyGo to a specific target (I'm targeting wasip2 but I guess it can be any target). Also, let's assume that I'm not planning to use any TLS related features in go-redis.

Currently, I cannot even compile when I'm using go-redis because VersionTLS constants and VersionName(version uint16) are missing, even though I'm not planning to use them (I grabbed the following code from redis-go and added netDevImplementation):

package main

import (
	"context"
	"fmt"

	"github.com/redis/go-redis/v9"
	netDevImplementation "path/to/netdev/implementation"
	"tinygo.org/x/drivers/netdev"
)

var ctx = context.Background()

func main() {
	netdev.UseNetdev(netDevImplementation.NewNetdev())
	rdb := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "", // no password set
		DB:       0,  // use default DB
	})

	err := rdb.Set(ctx, "key", "value", 0).Err()
	if err != nil {
		panic(err)
	}

	val, err := rdb.Get(ctx, "key").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println("key", val)

	val2, err := rdb.Get(ctx, "key2").Result()
	if err == redis.Nil {
		fmt.Println("key2 does not exist")
	} else if err != nil {
		panic(err)
	} else {
		fmt.Println("key2", val2)
	}
	// Output: key value
	// key2 does not exist
}
tinygo build -target=wasip2 --wit-package path/to/wit --wit-world cli main.go
# github.com/redis/go-redis/v9
../../../../go/pkg/mod/github.com/redis/go-redis/[email protected]/options.go:325:20: undefined: tls.VersionTLS12

Temporarily solution?

I know TLS is yet to be implemented for TinyGo, but I was wondering if we can add VersionTLS constants and VersionName(version uint16) for now so we can have modules like go-redis at least compiling.

Code to add (src/crypto/tls/common.go)

const (
	VersionTLS10 = 0x0301
	VersionTLS11 = 0x0302
	VersionTLS12 = 0x0303
	VersionTLS13 = 0x0304

	// Deprecated: SSLv3 is cryptographically broken, and is no longer
	// supported by this package. See golang.org/issue/32716.
	VersionSSL30 = 0x0300
)

// VersionName returns the name for the provided TLS version number
// (e.g. "TLS 1.3"), or a fallback representation of the value if the
// version is not implemented by this package.
func VersionName(version uint16) string {
	switch version {
	case VersionSSL30:
		return "SSLv3"
	case VersionTLS10:
		return "TLS 1.0"
	case VersionTLS11:
		return "TLS 1.1"
	case VersionTLS12:
		return "TLS 1.2"
	case VersionTLS13:
		return "TLS 1.3"
	default:
		return fmt.Sprintf("0x%04X", version)
	}
}
tinygo build -target=wasip2 --wit-package path/to/wit --wit-world cli main.go

Now it compiles and I can run it with a wasm runtime like Wasmtime.

wasmtime run -Shttp -Sinherit-network -Sallow-ip-name-lookup main.wasm

Output as expected:

key value
key2 does not exist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant