forked from gotopple/cf-origin-cert
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ffef9e
commit e72c85b
Showing
11 changed files
with
216 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package internal | ||
|
||
import "reflect" | ||
|
||
func IsInstanceOf(objectPtr, typePtr interface{}) bool { | ||
return reflect.TypeOf(objectPtr) == reflect.TypeOf(typePtr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package agent | ||
|
||
type CertificateWriter interface { | ||
Write(certKeyPair *CertKeyPair) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package agent | ||
|
||
import ( | ||
"io/ioutil" | ||
"log" | ||
) | ||
|
||
type FilesystemCertificateWriter struct { | ||
certOutputPath string | ||
keyOutputPath string | ||
lastID string | ||
} | ||
|
||
func NewFilesystemCertificateWriter(certOutputPath string, keyOutputPath string) CertificateWriter { | ||
return &FilesystemCertificateWriter{ | ||
certOutputPath: certOutputPath, | ||
keyOutputPath: keyOutputPath, | ||
} | ||
} | ||
|
||
func (w *FilesystemCertificateWriter) Write(certKeyPair *CertKeyPair) { | ||
if certKeyPair.ID == w.lastID { | ||
return | ||
} | ||
w.lastID = certKeyPair.ID | ||
|
||
err := ioutil.WriteFile(w.certOutputPath, certKeyPair.CertPEM, 0600) | ||
if err != nil { | ||
log.Printf("unable to write certificate file: %x", err) | ||
} | ||
|
||
err = ioutil.WriteFile(w.keyOutputPath, certKeyPair.Key, 0600) | ||
if err != nil { | ||
log.Printf("unable to write key file: %x", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package agent | ||
|
||
import ( | ||
"fmt" | ||
"github.com/gotopple/cf-origin-cert/internal" | ||
"github.com/ionrock/procs" | ||
"log" | ||
) | ||
|
||
type PostHookObserver struct { | ||
ch chan interface{} | ||
postHookCommand string | ||
} | ||
|
||
func NewPostHookObserver(command string) *PostHookObserver { | ||
result := &PostHookObserver{ | ||
make(chan interface{}, 2), | ||
command, | ||
} | ||
|
||
go func() { | ||
for { | ||
evt := <-result.ch | ||
if internal.IsInstanceOf(evt, (*CertKeyPair)(nil)) { | ||
result.onNewCertificate() | ||
} | ||
} | ||
}() | ||
|
||
return result | ||
} | ||
|
||
func (a *PostHookObserver) GetChannel() chan interface{} { | ||
return a.ch | ||
} | ||
|
||
func (a *PostHookObserver) onNewCertificate() { | ||
p := procs.NewProcess(a.postHookCommand) | ||
p.ErrHandler = func(line string) string { | ||
fmt.Printf("[POST HOOK - STD ERR] %s\n", line) | ||
return line | ||
} | ||
p.OutputHandler = func(line string) string { | ||
fmt.Printf("[POST HOOK - STD OUT] %s\n", line) | ||
return line | ||
} | ||
err := p.Run() | ||
|
||
if err != nil { | ||
log.Fatal("Post hook failed", err) | ||
} | ||
fmt.Println("Executed post hook") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package observer | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
type Observable struct { | ||
observers []chan interface{} | ||
mu *sync.Mutex | ||
} | ||
|
||
func MakeObservable() *Observable { | ||
return &Observable{ | ||
observers: make([]chan interface{}, 0), | ||
mu: &sync.Mutex{}, | ||
} | ||
} | ||
|
||
func (o *Observable) Attach(observer Observer) { | ||
o.mu.Lock() | ||
defer o.mu.Unlock() | ||
o.observers = append(o.observers, observer.GetChannel()) | ||
} | ||
|
||
func (o *Observable) Detach(observer Observer) { | ||
o.mu.Lock() | ||
defer o.mu.Unlock() | ||
for i, v := range o.observers { | ||
c := observer.GetChannel() | ||
if v == c { | ||
o.observers = append(o.observers[:i], o.observers[i+1:]...) | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (o *Observable) Notify(evt interface{}) { | ||
for _, v := range o.observers { | ||
v <- evt | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package observer | ||
|
||
type Observer interface { | ||
GetChannel() chan interface{} | ||
} |