Skip to content
This repository was archived by the owner on Apr 10, 2020. It is now read-only.

Commit ff7ed76

Browse files
author
ned
committed
Initial release
1 parent 6bbf6e4 commit ff7ed76

23 files changed

+6566
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
bin
2+
build
3+
scripts
4+
sweet.conf
5+
sweet-workspace
6+
workspace
7+
test-env

LICENSE

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
This software is distributed under the New BSD license.
2+
3+
Copyright (c) 2014, AppliedTrust, Inc.
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
* Neither the name of Sweet, the name of AppliedTrust, nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Makefile

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
VERSION=$(shell bin/sweet64 --version)
2+
3+
####
4+
linux32: bindata
5+
GOOS=linux GOARCH=386 go build -o bin/sweet32 cmd/main.go
6+
7+
all: bindata binaries packages
8+
9+
binaries: bindata linux32 linux64 darwin64
10+
11+
bindata:
12+
GOOS=linux GOARCH=amd64 $(GOPATH)/bin/go-bindata -pkg="sweet" tmpl/ static/
13+
14+
linux64: bindata
15+
GOOS=linux GOARCH=amd64 go build -o bin/sweet64 cmd/main.go
16+
17+
darwin64: bindata
18+
GOOS=darwin GOARCH=amd64 go build -o bin/sweet-osx cmd/main.go
19+
20+
####
21+
packages: rpm32 deb32 rpm64 deb64
22+
23+
rpm32:
24+
rm -rf build/rpm
25+
mkdir -p build/rpm/sweet/usr/local/bin
26+
cp bin/sweet32 build/rpm/sweet/usr/local/bin/sweet
27+
fpm -s dir -t rpm -n sweet -a i386 --epoch 0 -v $(VERSION) -C build/rpm/sweet .
28+
mv sweet-$(VERSION)-1.i386.rpm bin/
29+
30+
rpm64:
31+
rm -rf build/rpm
32+
mkdir -p build/rpm/sweet/usr/local/bin
33+
cp bin/sweet64 build/rpm/sweet/usr/local/bin/sweet
34+
fpm -s dir -t rpm -n sweet -a x86_64 --epoch 0 -v $(VERSION) -C build/rpm/sweet .
35+
mv sweet-$(VERSION)-1.x86_64.rpm bin/
36+
37+
deb32:
38+
rm -rf build/deb
39+
mkdir -p build/deb/sweet/usr/local/bin
40+
cp bin/sweet32 build/deb/sweet/usr/local/bin/sweet
41+
fpm -s dir -t deb -n sweet -a i386 -v $(VERSION) -C build/deb/sweet .
42+
mv sweet_$(VERSION)_i386.deb bin/
43+
44+
deb64:
45+
rm -rf build/deb
46+
mkdir -p build/deb/sweet/usr/local/bin
47+
cp bin/sweet64 build/deb/sweet/usr/local/bin/sweet
48+
fpm -s dir -t deb -n sweet -a amd64 -v $(VERSION) -C build/deb/sweet .
49+
mv sweet_$(VERSION)_amd64.deb bin/
50+
51+
####
52+
release:
53+
$(GOPATH)/bin/github-release release --user appliedtrust --repo sweet --tag $(VERSION) \
54+
--name "Sweet $(VERSION)" \
55+
--description "Network device configuration backups and change alerts for the 21st century." \
56+
--pre-release
57+
$(GOPATH)/bin/github-release upload --user appliedtrust --repo sweet --tag $(VERSION) \
58+
--name "sweet-linux-32" \
59+
--file bin/sweet32
60+
$(GOPATH)/bin/github-release upload --user appliedtrust --repo sweet --tag $(VERSION) \
61+
--name "sweet-linux-64" \
62+
--file bin/sweet64
63+
$(GOPATH)/bin/github-release upload --user appliedtrust --repo sweet --tag $(VERSION) \
64+
--name "sweet-osx" \
65+
--file bin/sweet-osx
66+
$(GOPATH)/bin/github-release upload --user appliedtrust --repo sweet --tag $(VERSION) \
67+
--name bin/sweet_$(VERSION)_i386.deb \
68+
--file bin/sweet_$(VERSION)_i386.deb
69+
$(GOPATH)/bin/github-release upload --user appliedtrust --repo sweet --tag $(VERSION) \
70+
--name bin/sweet-$(VERSION)-1.i386.rpm \
71+
--file bin/sweet-$(VERSION)-1.i386.rpm
72+

bindata.go

Lines changed: 4524 additions & 0 deletions
Large diffs are not rendered by default.

bindata_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package sweet
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"strings"
7+
"testing"
8+
)
9+
10+
func TestBindata(t *testing.T) {
11+
directories := []string{"tmpl", "static"}
12+
files := map[string]bool{}
13+
14+
// finding all bindata source files
15+
for _, dir := range directories {
16+
dirFiles, err := ioutil.ReadDir(dir)
17+
if err != nil {
18+
t.Errorf("Can't find bindata source directory: %s", dir)
19+
}
20+
for _, f := range dirFiles {
21+
files[dir+string(os.PathSeparator)+f.Name()] = true
22+
}
23+
}
24+
25+
// checking dashboard template file
26+
filename := "tmpl/index.html"
27+
if _, exists := files[filename]; !exists {
28+
t.Errorf("Critical bindata source file %s is missing", filename)
29+
}
30+
asset, err := Asset(filename)
31+
if err != nil {
32+
t.Errorf("Bindata is missing a critical file: %s", filename)
33+
}
34+
if !strings.Contains(string(asset), "Sweet status dashboard") {
35+
t.Errorf("%s is missing expected HTML meta tag", filename)
36+
}
37+
38+
// checking each source file is present in bindata
39+
for filename = range files {
40+
asset, err := Asset(filename)
41+
if err != nil {
42+
t.Errorf("Bindata is missing a file: %s", filename)
43+
}
44+
if len(asset) < 1 {
45+
t.Errorf("Zero-length bindata file: %s", filename)
46+
}
47+
}
48+
49+
}

cisco.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package sweet
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
func CollectCisco(device DeviceAccess) map[string]string {
9+
result := make(map[string]string)
10+
result["err"] = ""
11+
12+
c, err := newSSHCollector(device)
13+
if err != nil {
14+
result["err"] = fmt.Sprintf("%s: %s", device.Hostname, err.Error())
15+
return result
16+
}
17+
18+
if err := expect("assword:", c.Receive); err != nil {
19+
result["err"] = fmt.Sprintf("%s: %s", device.Hostname, err.Error())
20+
return result
21+
}
22+
c.Send <- device.Config["pass"] + "\n"
23+
multi := []string{"#", ">", "assword:"}
24+
m, err := expectMulti(multi, c.Receive)
25+
if err != nil {
26+
result["err"] = fmt.Sprintf("%s: %s", device.Hostname, err.Error())
27+
return result
28+
}
29+
if m == "assword:" { // bad pw
30+
result["err"] = fmt.Sprintf("%s: Bad login password.", device.Hostname)
31+
return result
32+
} else if m == ">" { // attempt enable
33+
c.Send <- "enable\n"
34+
if err := expect("assword:", c.Receive); err != nil {
35+
result["err"] = fmt.Sprintf("%s: %s", device.Hostname, err.Error())
36+
return result
37+
}
38+
c.Send <- device.Config["enable"] + "\n"
39+
if err := expect("#", c.Receive); err != nil {
40+
result["err"] = fmt.Sprintf("%s: %s", device.Hostname, err.Error())
41+
return result
42+
}
43+
}
44+
c.Send <- "terminal length 0\n"
45+
if err := expect("#", c.Receive); err != nil {
46+
result["err"] = fmt.Sprintf("%s: %s", device.Hostname, err.Error())
47+
return result
48+
}
49+
c.Send <- "terminal pager 0\n"
50+
if err := expect("#", c.Receive); err != nil {
51+
result["err"] = fmt.Sprintf("%s: %s", device.Hostname, err.Error())
52+
return result
53+
}
54+
c.Send <- "show conf\n"
55+
result["config"], err = timeoutSave(c.Receive, 500*time.Millisecond)
56+
if err != nil {
57+
result["err"] = fmt.Sprintf("%s: %s", device.Hostname, err.Error())
58+
return result
59+
}
60+
c.Send <- "exit\n"
61+
62+
return result
63+
}

cisco_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package sweet
2+
3+
import (
4+
"os"
5+
"strings"
6+
"testing"
7+
"time"
8+
)
9+
10+
func TestCiscoGood(t *testing.T) {
11+
d := new(DeviceAccess)
12+
d.Config = make(map[string]string)
13+
d.Method = "cisco"
14+
d.Timeout = 10 * time.Second
15+
16+
if os.Getenv("SWEET_TEST_HOST") == "" {
17+
t.Error("Test requries SWEET_TEST_HOST environment variable")
18+
return
19+
}
20+
if os.Getenv("SWEET_TEST_USER") == "" {
21+
t.Error("Test requries SWEET_TEST_USER environment variable")
22+
return
23+
}
24+
if os.Getenv("SWEET_TEST_PASS") == "" {
25+
t.Error("Test requries SWEET_TEST_PASS environment variable")
26+
return
27+
}
28+
29+
d.Hostname = os.Getenv("SWEET_TEST_HOST")
30+
d.Config["user"] = os.Getenv("SWEET_TEST_USER")
31+
d.Config["pass"] = os.Getenv("SWEET_TEST_PASS")
32+
33+
d.Target = d.Hostname
34+
35+
s := CollectCisco(*d)
36+
if !strings.Contains(s["config"], "aaa authorization commands") {
37+
t.Errorf("Config missing aaa line")
38+
}
39+
if !strings.Contains(s["config"], "ntp access-group peer") {
40+
t.Errorf("Config missing ntp line close to end")
41+
}
42+
43+
}

0 commit comments

Comments
 (0)