Skip to content

How To: Adding a New Status

Jon Prentice edited this page Apr 18, 2018 · 1 revision

How to Add a New Status

1. Modify seclab.go.

In func main(), change:

...

if len(os.Args)%4 != {
        fmt.Fprintf(os.Stderr, "usage: seclab key [dest open closed coffee NEWSTATE [..]]\n")
        return
}

...

Change iterator step value:

...

for i := 2; i+2 < len(os.Args); i += VAL {

...

Add new argument and modify Backend parameters:

        ...

        NEWfile := os.Args[i+PARAMVAL]
        s.AddBackend(backend.New(dest, openfile, closedfile, coffeefile, NEWfile))

        ...

2. Modify server/server.go.

Add a new byte:

      ...

      reqClose    = 0x00
      reqCoffee   = 0xcc
      reqNEW      = 0xVALID-BYTECODE
      reqKeygen   = 0xaa

      ...

Create a new status function:

...

func (s *server) FUNCNAME error {
        for _, b := range s.backend {
 		if err := b.NAME(); err != nil {
 			return err
                }
        return nil
}

...

Modify dispatchRequest function:

...

	} else if status == reqNAME {
		outLog.Print("Received request: close")
		return []byte{respAllGood}, s.FUNCNAME()
	} else if status == reqKeygen {

...

3. Modify backend/backend.go.

Add new function to Backend interface and fileBackend struct:

...

type Backend interface {
 	Open() error
 	Close() error
 	Coffee() error
 	NAME() error
}

type fileBackend struct {
 	linkPath   string
 	openPath   string
 	closedPath string
	coffeePath string
	NAMEPath string
}

...

Modify creation of new Backend instance to include new status image path:

...

func New(linkPath, openPath, closedPath, coffeePath string, NAMEPath string) Backend {
 	return &fileBackend{
 		linkPath:   linkPath,
 		openPath:   openPath,
 		closedPath: closedPath,
 		coffeePath: coffeePath,
 		NAMEPath: NAMEPath,
 	}
}

...

Create new fileBackend function:

...

func (b *fileBackend) NAME() error {
	return atomicLink(b.NAMEPath, b.linkPath)
}

...

4. Modify server/server_test.go.

Create new countingBackend function:

...

func (b *countingBackend) NAME() error {
	b.numClose += 1
	return nil
}

...

Create new errorBackend function:

...

func (b *errorBackend) NAME() error {
	return errors.New("NAME error")
}

...

5. Modify backend/backend_test.go.

Modify backend object within TestBadPaths function to add new parameter and execute new test function:

...

func TestBadPaths(t *testing.T) {
	b := backend.New("fakelink", "fakeopen", "fakeclose", "fakecoffee", "NAME")
	if err := b.Open(); err == nil {

...

	if err := b.NAME(); err == nil {
		t.Error("Expected LineError")
	}

...

Make necessary changes to TestLink function:

...

	closed := filepath.Join(tempDir, "close.txt")
	coffee := filepath.Join(tempDir, "coffee.txt")
	NAME := filepath.Join(tempDir, "NAME.txt")
	if err := ioutil.WriteFile(open, []byte("Lab Open"), 0644); err != nil {

...

	if err := ioutil.WriteFile(NAME, []byte("STATUS MSG"), 0644); err != nil {
		t.Fatalf("WriteFile: %v", err)
	}

...

	b := backend.New(link, open, closed, coffee, NAME)

...

	// Try to NAME
	if err := b.NAME(); err != nil {
		t.Error(err)
	}
	data, err = ioutil.ReadFile(link)
	if err != nil {
		t.Error(err)
	} else if bytes.Compare(data, []byte("STATUS MSG")) != 0 {
		t.Error("Expected STATUS MSG, got", string(data))
	}
}

6. Copy badge file and change perms.

  1. Create badge from
    https://img.shields.io/badge/lab-NAME-8D6E63.svg

(8D6E63 can be tweaked to some other hex color)

  1. Copy badge file to /var/www/seclab/

  2. Create compressed version:

zopfli FILENAME
  1. Make sure permissions are set correctly:
sudo chown seclab:seclab FILENAME

7. Success

Profit!

πŸ‘ πŸ‘ πŸ‘ πŸ‘ πŸ‘ πŸ‘ πŸ‘