Skip to content

Conversation

@iamargus95
Copy link
Contributor

@iamargus95 iamargus95 commented Apr 27, 2023

  • Generate config from env vars.
  • Sets config correctly when multiple blocks of the same Provider are present.
  • Set all types of config values
    • String
    • Bool
    • []string
    • map[string]string

Here is the config.yaml I used while testing locally.

slack:
  - id: recon
    slack_channel: $SLACK_CHANNEL_1
    slack_username: $SLACK_USER_1
    slack_webhook_url: $SLACK_WEBHOOK_1
    slack_threads: $SLACK_THREADS_1
  - id: recon1
    slack_channel: $SLACK_CHANNEL_2
    slack_username: $SLACK_USER_2
    slack_webhook_url: $SLACK_WEBHOOK_2
    slack_threads: $SLACK_THREADS_2
pushover:
  - id: $PUSHOVER_ID
    pushover_api_token: $PUSH_API_TOKEN
    pushover_devices: $PUSH_DEVICES
custom:
  - id: $CUSTOM_ID
    custom_headers: $CUSTOM_HEADERS

These are the export statements I used to set the ENV variables.

export SLACK_CHANNEL_1=slackChannel1
export SLACK_USER_1=slackUser1
export SLACK_WEBHOOK_1=slackWebhook1
export SLACK_THREADS_1=true
export SLACK_CHANNEL_2=slackChannel2
export SLACK_USER_2=slackUser2
export SLACK_WEBHOOK_2=slackWebhook2
export SLACK_THREADS_2=false
export PUSHOVER_ID=pushoverID
export PUSH_API_TOKEN=123qwerty123helloworld
export PUSH_DEVICES='["device1","device2","device3"]'
export CUSTOM_ID=customID
export CUSTOM_HEADERS='{"Content-Type":"application/json","X-Api-Key": "XXXXX"}'

@ehsandeep ehsandeep linked an issue Apr 27, 2023 that may be closed by this pull request
@iamargus95
Copy link
Contributor Author

iamargus95 commented Apr 28, 2023

Found 2 different approaches to solve this issue:

  • Use Walkfunc with callback and expandEnvVars like in Nuclei.
  • Create a temp config file and replace the words that start with $ and then decode using yaml library.

The problem I'm facing is that the yaml decoder does not set boolean values in any of the following cases:

  • Set in environment variables.
  • Hardcoded in the config file.

It always defaults to false.

I've tried using both the std lib pkg and the yamlutil used in Nuclei.
Any suggestions?
CC: @ehsandeep @Mzack9999 @tarunKoyalwar

@ShubhamRasal
Copy link

we can go with second approach..

// utils.go
func readProviderConfig(filepath string) (io.Reader, error) {
	// Open the file
	file, err := os.Open(filepath)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	// Create a scanner to read the file line by line
	scanner := bufio.NewScanner(file)

	// Create a string builder to accumulate the modified data
	var sb strings.Builder

	// Iterate over each line and do variable substitution
	for scanner.Scan() {
		line := scanner.Text()
		newLine := substituteEnvVars(line)
		sb.WriteString(newLine)
		sb.WriteString("\n")
	}
	// Check for errors
	if err := scanner.Err(); err != nil {
		return nil, err
	}

	return strings.NewReader(sb.String()), nil
}

func substituteEnvVars(line string) string {
	for _, word := range strings.Fields(line) {
		word = strings.Trim(word, " \"")
		if strings.HasPrefix(word, "$") {
			key := strings.TrimPrefix(word, "$")
			val := os.Getenv(key)
			line = strings.ReplaceAll(line, word, val)
		}
	}
	return line
}

runner.go

	reader, err := readProviderConfig(options.ProviderConfig)
	if err != nil {
		return nil, errors.Wrap(err, "could not open provider config file")
	}
	if parseErr := yaml.NewDecoder(reader).Decode(&providerOptions); parseErr != nil {
		return nil, errors.Wrap(parseErr, "could not parse provider config file")
	}

@iamargus95
Copy link
Contributor Author

iamargus95 commented May 8, 2023

@ShubhamRasal

  • Thank you for the suggestion. It does solve the problem I was facing.
  • All datatypes that are part of the providerOptions struct can be parsed.

@iamargus95 iamargus95 marked this pull request as ready for review May 8, 2023 07:18
@ShubhamRasal ShubhamRasal requested a review from Mzack9999 May 8, 2023 13:03
@ShubhamRasal ShubhamRasal marked this pull request as draft May 8, 2023 13:10
@iamargus95 iamargus95 marked this pull request as ready for review May 9, 2023 05:37
@tarunKoyalwar tarunKoyalwar requested review from tarunKoyalwar and removed request for Mzack9999 May 9, 2023 07:38
Copy link
Member

@tarunKoyalwar tarunKoyalwar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice work @iamargus95 , lgtm ! verified locally and seems to work
ex config

cat ~/.config/notify/provider-config.yaml
discord:
  - id: "crawl"
    discord_channel: "crawl"
    discord_username: "test"
    discord_format: "{{data}}"
    discord_webhook_url: "$DISCORD_WEBHOOK_URL"

@tarunKoyalwar
Copy link
Member

@iamargus95 , since this is a generic implementation what do you think of creating new PR to move this function to https://github.com/projectdiscovery/utils/tree/main/file . that way we can extend this support to other projects

Thanks

Signed-off-by: iamargus95 <[email protected]>
@iamargus95
Copy link
Contributor Author

@tarunKoyalwar
Kindly review this PR in utils.
I've added this functionality as a helper.

@ehsandeep ehsandeep requested a review from tarunKoyalwar May 15, 2023 09:43
Copy link
Member

@tarunKoyalwar tarunKoyalwar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iamargus95 , since upstream pr projectdiscovery/utils#149 is merged now can you update this PR to use those helper utils

@iamargus95
Copy link
Contributor Author

@iamargus95 , since upstream pr projectdiscovery/utils#149 is merged now can you update this PR to use those helper utils

@tarunKoyalwar I think the latest release (v0.0.30) does not have the newly added helper function. Waiting for it to be added.

@ehsandeep
Copy link
Member

@iamargus95 , since upstream pr projectdiscovery/utils#149 is merged now can you update this PR to use those helper utils

@tarunKoyalwar I think the latest release (v0.0.30) does not have the newly added helper function. Waiting for it to be added.

https://github.com/projectdiscovery/utils/releases/tag/v0.0.31

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@ehsandeep ehsandeep merged commit f057faa into projectdiscovery:dev May 27, 2023
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 this pull request may close these issues.

Reading keys from environment variables

4 participants