Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alxrem committed Apr 5, 2023
0 parents commit 1e5dd7a
Show file tree
Hide file tree
Showing 10 changed files with 994 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.idea/
/vendor/
/terraform-provider-jsonnet
/dist/
40 changes: 40 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
before:
hooks:
- go mod tidy
builds:
- env:
- CGO_ENABLED=0
goos:
- freebsd
- windows
- linux
- darwin
goarch:
- amd64
- '386'
- arm
- arm64
ignore:
- goos: darwin
goarch: '386'
binary: '{{ .ProjectName }}_v{{ .Version }}'
archives:
- format: zip
name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}'
checksum:
name_template: '{{ .ProjectName }}_{{ .Version }}_SHA256SUMS'
algorithm: sha256
signs:
- artifacts: checksum
args:
- "--local-user"
- "A17BA510E9BD6100D6E6532C2FB63D48D5AB04AE" # Replace this with your GPG signing key ID
- "--output"
- "${signature}"
- "--detach-sign"
- "${artifact}"
release:
# Visit your project's GitHub Releases page to publish this release.
draft: true
changelog:
skip: true
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## 0.1.0 (Apr 5, 2023)

NOTES:

First public release.
373 changes: 373 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module gitlab.com/sport24ru/terraform-provider-dex

go 1.19

require (
github.com/golang/protobuf v1.5.2
github.com/hashicorp/terraform-plugin-framework v1.2.0
github.com/hashicorp/terraform-plugin-go v0.14.3
github.com/hashicorp/terraform-plugin-testing v1.2.0
)

require (
github.com/agext/levenshtein v1.2.2 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-plugin v1.4.8 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/hc-install v0.5.0 // indirect
github.com/hashicorp/hcl/v2 v2.16.2 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.18.1 // indirect
github.com/hashicorp/terraform-json v0.16.0 // indirect
github.com/hashicorp/terraform-plugin-log v0.8.0 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.26.1 // indirect
github.com/hashicorp/terraform-registry-address v0.1.0 // indirect
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
github.com/vmihailenco/tagparser v0.1.1 // indirect
github.com/zclconf/go-cty v1.13.1 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/genproto v0.0.0-20200711021454-869866162049 // indirect
google.golang.org/grpc v1.51.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
330 changes: 330 additions & 0 deletions go.sum

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"context"
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"gitlab.com/sport24ru/terraform-provider-dex/private/provider"
"log"
)

func main() {
if err := providerserver.Serve(context.Background(), provider.NewDexProvider, providerserver.ServeOpts{
Address: "registry.terraform.io/sport24ru/dex",
}); err != nil {
log.Fatal(err)
}
}
95 changes: 95 additions & 0 deletions private/provider/data_source_dex_sub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package provider

import (
"context"
"encoding/base64"
"github.com/golang/protobuf/proto"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var _ datasource.DataSource = &DexSubDataSource{}

type DexSubDataSource struct{}

type DexSubDataSourceModel struct {
Id types.String `tfsdk:"id"`
UserId types.String `tfsdk:"user_id"`
ConnId types.String `tfsdk:"conn_id"`
Sub types.String `tfsdk:"sub"`
}

func (d DexSubDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_sub"
}

func (d DexSubDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"user_id": schema.StringAttribute{
Required: true,
MarkdownDescription: "User ID of Dex ID token.",
},
"conn_id": schema.StringAttribute{
Required: true,
MarkdownDescription: "Conn ID of Dex ID token.",
},
"sub": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Subject string of Dex ID token.",
},
},
}
}

type IDTokenSubject struct {
UserId string `protobuf:"bytes,1,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"`
ConnId string `protobuf:"bytes,2,opt,name=conn_id,json=connId,proto3" json:"conn_id,omitempty"`
}

func (m *IDTokenSubject) Reset() { *m = IDTokenSubject{} }
func (m *IDTokenSubject) String() string { return proto.CompactTextString(m) }
func (*IDTokenSubject) ProtoMessage() {}

func Marshal(message proto.Message) (string, error) {
data, err := proto.Marshal(message)
if err != nil {
return "", err
}
return base64.RawURLEncoding.EncodeToString(data), nil
}

func (d DexSubDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state DexSubDataSourceModel

resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

var idTokenSubject IDTokenSubject

idTokenSubject.ConnId = state.ConnId.ValueString()
idTokenSubject.UserId = state.UserId.ValueString()

sub, err := Marshal(&idTokenSubject)
if err != nil {
panic(err)
}

state.Sub = types.StringValue(sub)
state.Id = types.StringValue(sub)

resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
}

func NewDexSubDataSource() datasource.DataSource {
return &DexSubDataSource{}
}
40 changes: 40 additions & 0 deletions private/provider/data_source_dex_sub_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package provider

import (
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"testing"
)

var (
testAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
"dex": providerserver.NewProtocol6WithError(NewDexProvider()),
}
)

func TestDexSubDataSource_Subjects(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
data "dex_sub" "gitlab" {
user_id = "vpupkin"
conn_id = "gitlab"
}
data "dex_sub" "github" {
user_id = "vasily.pupkin"
conn_id = "github"
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.dex_sub.gitlab", "sub", "Cgd2cHVwa2luEgZnaXRsYWI"),
resource.TestCheckResourceAttr("data.dex_sub.github", "sub", "Cg12YXNpbHkucHVwa2luEgZnaXRodWI"),
),
},
},
})

}
34 changes: 34 additions & 0 deletions private/provider/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package provider

import (
"context"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/resource"
)

type DexProvider struct{}

func (p DexProvider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
resp.TypeName = "dex"
}

func (p DexProvider) Schema(_ context.Context, _ provider.SchemaRequest, _ *provider.SchemaResponse) {
}

func (p DexProvider) Configure(_ context.Context, _ provider.ConfigureRequest, _ *provider.ConfigureResponse) {
}

func (p DexProvider) DataSources(_ context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewDexSubDataSource,
}
}

func (p DexProvider) Resources(_ context.Context) []func() resource.Resource {
return nil
}

func NewDexProvider() provider.Provider {
return &DexProvider{}
}

0 comments on commit 1e5dd7a

Please sign in to comment.