-
Notifications
You must be signed in to change notification settings - Fork 959
Expand file tree
/
Copy pathdata_source_github_app_token.go
More file actions
83 lines (75 loc) · 2.29 KB
/
data_source_github_app_token.go
File metadata and controls
83 lines (75 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package github
import (
"context"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceGithubAppToken() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceGithubAppTokenRead,
Schema: map[string]*schema.Schema{
"app_id": {
Type: schema.TypeString,
Required: true,
Description: descriptions["app_auth.id"],
},
"installation_id": {
Type: schema.TypeString,
Required: true,
Description: descriptions["app_auth.installation_id"],
},
"pem_file": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Description: descriptions["app_auth.pem_file"],
ExactlyOneOf: []string{"pem_file", "aws_kms_key_id"},
},
"aws_kms_key_id": {
Type: schema.TypeString,
Optional: true,
Description: descriptions["app_auth.aws_kms_key_id"],
ExactlyOneOf: []string{"pem_file", "aws_kms_key_id"},
},
"token": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
Description: "The generated token from the credentials.",
},
},
}
}
func dataSourceGithubAppTokenRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
appID := d.Get("app_id").(string)
installationID := d.Get("installation_id").(string)
var signer Signer
var err error
if v, ok := d.GetOk("aws_kms_key_id"); ok {
signer, err = NewAWSKMSSigner(ctx, v.(string))
if err != nil {
return diag.FromErr(err)
}
} else {
// The Go encoding/pem package only decodes PEM formatted blocks
// that contain new lines. Some platforms, like Terraform Cloud,
// do not support new lines within Environment Variables.
// Any occurrence of \n in the `pem_file` argument's value is replaced
// with an actual new line character before decoding.
pemFile := strings.ReplaceAll(d.Get("pem_file").(string), `\n`, "\n")
signer, err = NewPEMSigner([]byte(pemFile))
if err != nil {
return diag.FromErr(err)
}
}
token, err := GenerateOAuthTokenFromApp(ctx, signer, meta.(*Owner).v3client.BaseURL, appID, installationID)
if err != nil {
return diag.FromErr(err)
}
if err := d.Set("token", token); err != nil {
return diag.FromErr(err)
}
d.SetId("id")
return nil
}