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

Force exit on timeout #7

Merged
merged 1 commit into from
May 29, 2020
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.1.1] - 2020-05-29
### Changed
- kubectl-login will now exit after 10 minutes of idling. This in order to prevent the program from staying in the
background if left unattended.
- Added a sleep in the main loop to avoid hogging the CPU while active.

## [1.1.0] - 2020-02-12
### Changed
- Token is now stored outside of `KUBECONFIG` to avoid it being sent when expired, as described in the kubernetes issue
Expand Down
5 changes: 3 additions & 2 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"net/http"
"os"
"time"

"github.com/Bisnode/kubectl-login/util"
Expand Down Expand Up @@ -90,8 +91,8 @@ func (h *IDTokenWebhookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
}

if !h.ExecCredentialMode {
fmt.Println(fmt.Sprintf(
"Authenticated for context %v. Token valid until %v.", h.ClientCfg.CurrentContext, exp))
_, _ = fmt.Fprintf(os.Stdout,
"Authenticated for context %v. Token valid until %v.\n", h.ClientCfg.CurrentContext, exp)
}

// Return control to shell at this point
Expand Down
14 changes: 11 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func parseArgs(clientCfg *api.Config) (forceLogin bool, execCredentialMode bool,
}

if flag.NArg() > 0 {
_, _ = fmt.Fprint(os.Stderr, fmt.Sprintf("Unrecognized parameter(s): %v\n", flag.Args()))
_, _ = fmt.Fprintf(os.Stderr, "Unrecognized parameter(s): %v\n", flag.Args())
flag.Usage()
os.Exit(1)
}
Expand All @@ -135,6 +135,12 @@ func currentToken(clientCfg *api.Config) string {
func main() {
quitChan := make(chan struct{})
sigChan := make(chan os.Signal, 1)
timeoutChan := make(chan bool, 1)
go func() {
time.Sleep(10 * time.Minute)
timeoutChan <- true
anderseknert marked this conversation as resolved.
Show resolved Hide resolved
}()

signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)

cluster := api.NewCluster()
Expand Down Expand Up @@ -181,8 +187,7 @@ func main() {
authzEndpointURL, _ := url.Parse(issuer.AuthorizeEndpoint)
_, err = net.LookupIP(authzEndpointURL.Host)
if err != nil {
fmt.Println(fmt.Sprintf("Could not resolve %v. Are you on the office network / VPN?", authzEndpointURL.Host))
os.Exit(1)
log.Fatalf("Could not resolve %v. Are you on the office network / VPN?", authzEndpointURL.Host)
}

nonce := util.RandomString(12)
Expand Down Expand Up @@ -239,7 +244,10 @@ func main() {
return
case <-sigChan:
close(quitChan)
case <-timeoutChan:
log.Fatal("kubetcl-login aborting after idling for 10 minutes")
default:
time.Sleep(100 * time.Millisecond)
}
}
}