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

Add initial OAuth2 auth support #335

Merged
merged 1 commit into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
12 changes: 6 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@
# Generated binaries, checksums
/release_assets

# Ignore binaries generated by pre-v0.1.0 builds
/check-mail

# Ignore binaries at root of repo (often generated here when testing)
/check_imap_mailbox
/check_imap_mailbox_basic
/check_imap_mailbox_oauth2
/list-emails
/lsimap
/xoauth2

# Help prevent accidentally including this credentials file in the repo
/accounts.ini
accounts.ini

# Ignore log and report files generated by the list-emails application
/output
/log
output/
log/
22 changes: 13 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
SHELL = /bin/bash

# Space-separated list of cmd/BINARY_NAME directories to build
WHAT = check_imap_mailbox list-emails lsimap
WHAT = check_imap_mailbox_basic \
check_imap_mailbox_oauth2 \
list-emails \
lsimap \
xoauth2 \

# TODO: This will need to be standardized across all cmd files in order to
# work as intended.
Expand Down Expand Up @@ -196,19 +200,19 @@ windows:

@for target in $(WHAT); do \
mkdir -p $(OUTPUTDIR)/$$target && \
echo "Running go generate for $$target 386 binary ..." && \
echo " Running go generate for $$target 386 binary ..." && \
cd ./cmd/$$target && \
env GOOS=windows GOARCH=386 go generate && \
cd $$OLDPWD && \
echo "Building $$target 386 binary" && \
echo " Building $$target 386 binary" && \
env GOOS=windows GOARCH=386 $(BUILDCMD) -o $(OUTPUTDIR)/$$target/$$target-$(VERSION)-windows-386.exe ./cmd/$$target && \
echo "Running go generate for $$target amd64 binary ..." && \
echo " Running go generate for $$target amd64 binary ..." && \
cd ./cmd/$$target && \
env GOOS=windows GOARCH=amd64 go generate && \
cd $$OLDPWD && \
echo "Building $$target amd64 binary" && \
echo " Building $$target amd64 binary" && \
env GOOS=windows GOARCH=amd64 $(BUILDCMD) -o $(OUTPUTDIR)/$$target/$$target-$(VERSION)-windows-amd64.exe ./cmd/$$target && \
echo "Generating $$target checksum files" && \
echo " Generating $$target checksum files" && \
cd $(OUTPUTDIR)/$$target && \
$(CHECKSUMCMD) $$target-$(VERSION)-windows-386.exe > $$target-$(VERSION)-windows-386.exe.sha256 && \
$(CHECKSUMCMD) $$target-$(VERSION)-windows-amd64.exe > $$target-$(VERSION)-windows-amd64.exe.sha256 && \
Expand All @@ -225,11 +229,11 @@ linux:

@for target in $(WHAT); do \
mkdir -p $(OUTPUTDIR)/$$target && \
echo "Building $$target 386 binary" && \
echo " Building $$target 386 binary" && \
env GOOS=linux GOARCH=386 $(BUILDCMD) -o $(OUTPUTDIR)/$$target/$$target-$(VERSION)-linux-386 ./cmd/$$target && \
echo "Building $$target amd64 binary" && \
echo " Building $$target amd64 binary" && \
env GOOS=linux GOARCH=amd64 $(BUILDCMD) -o $(OUTPUTDIR)/$$target/$$target-$(VERSION)-linux-amd64 ./cmd/$$target && \
echo "Generating $$target checksum files" && \
echo " Generating $$target checksum files" && \
cd $(OUTPUTDIR)/$$target && \
$(CHECKSUMCMD) $$target-$(VERSION)-linux-386 > $$target-$(VERSION)-linux-386.sha256 && \
$(CHECKSUMCMD) $$target-$(VERSION)-linux-amd64 > $$target-$(VERSION)-linux-amd64.sha256 && \
Expand Down
351 changes: 304 additions & 47 deletions README.md

Large diffs are not rendered by default.

181 changes: 0 additions & 181 deletions cmd/check_imap_mailbox/main.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
// Licensed under the MIT License. See LICENSE file in the project root for
// full license information.

// Small CLI app used to generate listing of mailbox contents.
// Nagios plugin used to monitor mailboxes for items. Uses Basic Auth to
// authenticate.
//
// See our [GitHub repo]:
//
Expand Down
122 changes: 122 additions & 0 deletions cmd/check_imap_mailbox_basic/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright 2022 Adam Chalkley
//
// https://github.com/atc0005/check-mail
//
// Licensed under the MIT License. See LICENSE file in the project root for
// full license information.

//go:generate go-winres make --product-version=git-tag --file-version=git-tag

package main

import (
"context"
"errors"
"fmt"
"time"

zlog "github.com/rs/zerolog/log"

"github.com/atc0005/check-mail/internal/config"
"github.com/atc0005/go-nagios"
)

func main() {

ctx := context.Background()

// Start off assuming all is well, adjust as we go.
var nagiosExitState = nagios.ExitState{
LastError: nil,
ExitStatusCode: nagios.StateOKExitCode,
}

// defer this from the start so it is the last deferred function to run
defer nagiosExitState.ReturnCheckResults()

// Setup configuration by parsing user-provided flags.
cfg, cfgErr := config.New(config.AppType{PluginIMAPMailboxBasicAuth: true})
switch {
case errors.Is(cfgErr, config.ErrVersionRequested):
fmt.Println(config.Version())

return

case cfgErr != nil:
// We're using the standalone Err function from rs/zerolog/log as we
// do not have a working configuration.
zlog.Err(cfgErr).Msg("Error initializing application")
nagiosExitState.ServiceOutput = fmt.Sprintf(
"%s: Error initializing application",
nagios.StateCRITICALLabel,
)
nagiosExitState.AddError(cfgErr)
nagiosExitState.ExitStatusCode = nagios.StateCRITICALExitCode

return
}

if cfg.EmitBranding {
// If enabled, show application details at end of notification
nagiosExitState.BrandingCallback = config.Branding("Notification generated by ")
}

// NOTE: This plugin is still intended for checking a single account, but
// sufficient work is in place to allow bulk processing if there is
// sufficient interest.
for i, account := range cfg.Accounts {

logger := cfg.Log.With().
Str("username", account.Username).
Str("server", account.Server).
Int("port", account.Port).
Str("folders_to_check", account.Folders.String()).
Logger()

// processAccount is responsible for setting the nagios.ExitState
// values, logging errors, etc.
results, err := processAccount(ctx, account, cfg, &nagiosExitState, logger)
if err != nil {
return
}

// Evaluate whether anything was found and sound an alert if so
if results.GotMail() {
logger.Debug().Msgf("%d messages found: %s",
results.TotalMessagesFound(),
results.MessagesFoundSummary(),
)
nagiosExitState.ServiceOutput = fmt.Sprintf(
"%s: %s: %d messages found: %s",
nagios.StateWARNINGLabel,
account.Username,
results.TotalMessagesFound(),
results.MessagesFoundSummary(),
)
nagiosExitState.ExitStatusCode = nagios.StateWARNINGExitCode
return
}

if i+1 < len(cfg.Accounts) {
// Delay processing the next account (unless we've processed them
// all) in an attempt to prevent encountering the "User is
// authenticated but not connected" error that is believed to
// occur when remote connections limit is exceeded.
time.Sleep(cfg.AccountProcessDelay())
}
}

// Give the all clear: no mail was found
cfg.Log.Debug().Msg("No messages found to report")

// these values are known, consistent regardless of checking one or many
// accounts
nagiosExitState.ExitStatusCode = nagios.StateOKExitCode

// customize ServiceOutput and LongServiceOutput based on number of
// specified accounts
setSummary(cfg.Accounts, &nagiosExitState)

// implied return here :)

}
Loading