Skip to content

Commit fa9cb16

Browse files
committed
chore: convert htpasswd settings to block
1 parent 957ceed commit fa9cb16

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed

docs/sources/reference/components/otelcol/otelcol.auth.basic.md

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,48 @@ otelcol.auth.basic "<LABEL>" {
3232
username = "<USERNAME>"
3333
password = "<PASSWORD>"
3434
35-
htpasswd_file = "/etc/alloy/.htpasswd"
35+
htpasswd {
36+
file = "/etc/alloy/.htpasswd"
37+
inline = "<USERNAME>:<PASSWORD>"
38+
}
3639
}
3740
```
3841

3942
## Arguments
4043

4144
You can use the following arguments with `otelcol.auth.basic`:
4245

43-
| Name | Type | Description | Default | Required |
44-
|-----------------|----------|---------------------------------------------------------------------------------|---------|----------|
45-
| `password` | `secret` | Password to use for basic authentication requests. | | no |
46-
| `username` | `string` | Username to use for basic authentication requests. | | no |
47-
| `htpasswd_file` | `string` | File to use for basic authentication requests. It can be used in receivers only | | no |
46+
| Name | Type | Description | Default | Required |
47+
|------------|----------|---------------------------------------------------------------------------------|---------|----------|
48+
| `password` | `secret` | Password to use for basic authentication requests. | | no |
49+
| `username` | `string` | Username to use for basic authentication requests. | | no |
4850

4951

5052
## Blocks
5153

5254
You can use the following block with `otelcol.auth.basic`:
5355

5456
| Block | Description | Required |
55-
| -------------------------------- | -------------------------------------------------------------------------- | -------- |
57+
|----------------------------------|----------------------------------------------------------------------------|----------|
5658
| [`debug_metrics`][debug_metrics] | Configures the metrics that this component generates to monitor its state. | no |
59+
| [`htpasswd`][htpasswd] | Configures the service authentication for a receiver | no |
5760

5861
[debug_metrics]: #debug_metrics
62+
[htpasswd]: #htpasswd
5963

6064
### `debug_metrics`
6165

6266
{{< docs/shared lookup="reference/components/otelcol-debug-metrics-block.md" source="alloy" version="<ALLOY_VERSION>" >}}
6367

68+
### `htpasswd`
69+
70+
The `htpasswd` block configures how the server extensions will authenticate calls.
71+
72+
| Name | Type | Description | Default | Required |
73+
|----------|----------|--------------------------------------------------------------------|---------|----------|
74+
| `file` | `string` | Path to the htpasswd file to use for basic authentication requests | `""` | no |
75+
| `inline` | `string` | The htpasswd file inline content | `""` | no |
76+
6477
## Exported fields
6578

6679
The following fields are exported and can be referenced by other components:
@@ -148,7 +161,9 @@ otelcol.receiver.otlp "example" {
148161
otelcol.exporter.debug "default" {}
149162
150163
otelcol.auth.basic "creds" {
151-
htpasswd_file = "/etc/alloy/.htpasswd"
164+
htpasswd {
165+
file = "/etc/alloy/.htpasswd"
166+
}
152167
}
153168
```
154169

@@ -178,7 +193,9 @@ otelcol.auth.basic "creds" {
178193
username = "demo"
179194
password = sys.env("API_KEY")
180195
181-
htpasswd_file = "/etc/alloy/.htpasswd"
196+
htpasswd {
197+
file = "/etc/alloy/.htpasswd"
198+
}
182199
}
183200
```
184201

internal/component/otelcol/auth/basic/basic.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,28 @@ func init() {
3535
})
3636
}
3737

38+
type HtpasswdConfig struct {
39+
File string `alloy:"file,attr,optional"`
40+
Inline string `alloy:"inline,attr,optional"`
41+
}
42+
43+
func (c HtpasswdConfig) convert() *basicauthextension.HtpasswdSettings {
44+
settings := &basicauthextension.HtpasswdSettings{}
45+
if c.File != "" {
46+
settings.File = c.File
47+
}
48+
if c.Inline != "" {
49+
settings.Inline = c.Inline
50+
}
51+
return settings
52+
}
53+
3854
// Arguments configures the otelcol.auth.basic component.
3955
type Arguments struct {
4056
Username string `alloy:"username,attr,optional"`
4157
Password alloytypes.Secret `alloy:"password,attr,optional"`
4258

43-
HtpasswdFile string `alloy:"htpasswd_file,attr,optional"`
59+
Htpasswd *HtpasswdConfig `alloy:"htpasswd,block,optional"`
4460

4561
// DebugMetrics configures component internal metrics. Optional.
4662
DebugMetrics otelcolCfg.DebugMetricsArguments `alloy:"debug_metrics,block,optional"`
@@ -56,7 +72,7 @@ func (args *Arguments) SetToDefault() {
5672
// Validate implements syntax.Validator
5773
func (args Arguments) Validate() error {
5874
// check if no argument was provided
59-
if args.Username == "" && args.Password == "" && args.HtpasswdFile == "" {
75+
if args.Username == "" && args.Password == "" && args.Htpasswd == nil {
6076
return errNoCredentialSource
6177
}
6278
// the downstream basicauthextension package supports having both inline
@@ -86,11 +102,11 @@ func (args Arguments) ConvertServer() (otelcomponent.Config, error) {
86102
c := &basicauthextension.Config{
87103
Htpasswd: &basicauthextension.HtpasswdSettings{},
88104
}
89-
if args.HtpasswdFile != "" {
90-
c.Htpasswd.File = args.HtpasswdFile
105+
if args.Htpasswd != nil {
106+
c.Htpasswd = args.Htpasswd.convert()
91107
}
92108
if args.Username != "" && args.Password != "" {
93-
c.Htpasswd.Inline = fmt.Sprintf("%s:%s", args.Username, args.Password)
109+
c.Htpasswd.Inline += fmt.Sprintf("\n%s:%s", args.Username, args.Password)
94110
}
95111

96112
return c, nil

internal/component/otelcol/auth/basic/basic_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ var (
3535
cfg = fmt.Sprintf(`
3636
username = "%s"
3737
password = "%s"
38-
htpasswd_file = "%s"
38+
htpasswd = {
39+
file = "%s"
40+
}
3941
`, actualUsername, actualPassword, htpasswdPath)
4042
)
4143

0 commit comments

Comments
 (0)