예제 #1
0
func domainCacheTest(database *mgo.Database, r *http.Request,
	requestContent, header string, domainCacheTestData []DomainCacheTestData) {

	mux := handy.NewHandy()

	h := new(handler.DomainHandler)
	mux.Handle("/domain/{fqdn}", func() handy.Handler {
		return h
	})

	for _, item := range domainCacheTestData {
		r.Header.Set(header, item.HeaderValue)

		if len(requestContent) > 0 {
			utils.BuildHTTPHeader(r, []byte(requestContent))
		} else {
			utils.BuildHTTPHeader(r, nil)
		}

		w := httptest.NewRecorder()
		mux.ServeHTTP(w, r)

		responseContent, err := ioutil.ReadAll(w.Body)
		if err != nil {
			utils.Fatalln("Error reading response body", err)
		}

		if w.Code != item.ExpectedHTTPStatus {
			utils.Fatalln(fmt.Sprintf("Error in %s test using %s [%s] "+
				"HTTP header. Expected HTTP status code %d and got %d",
				r.Method, header, item.HeaderValue, item.ExpectedHTTPStatus,
				w.Code), errors.New(string(responseContent)))
		}
	}
}
예제 #2
0
func retrieveUnknownScan(database *mgo.Database, startedAt time.Time) {
	mux := handy.NewHandy()

	h := new(handler.ScanHandler)
	mux.Handle("/scan/{started-at}", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", fmt.Sprintf("/scan/%s", startedAt.Add(-1*time.Millisecond).Format(time.RFC3339Nano)), nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusNotFound {
		utils.Fatalln("Error retrieving unknown scan", errors.New(string(responseContent)))
	}
}
예제 #3
0
func main() {
	flag.Parse()

	var config ScanCollectorTestConfigFile
	err := utils.ReadConfigFile(configFilePath, &config)

	if err == utils.ErrConfigFileUndefined {
		fmt.Println(err.Error())
		fmt.Println("Usage:")
		flag.PrintDefaults()
		return

	} else if err != nil {
		utils.Fatalln("Error reading configuration file", err)
	}

	database, databaseSession, err := mongodb.Open(
		[]string{config.Database.URI},
		config.Database.Name,
		false, "", "",
	)
	if err != nil {
		utils.Fatalln("Error connecting the database", err)
	}
	defer databaseSession.Close()

	// Remove all data before starting the test. This is necessary because maybe in the last
	// test there was an error and the data wasn't removed from the database
	utils.ClearDatabase(database)

	domainWithErrors(config, database)
	domainWithNoErrors(config, database)

	utils.Println("SUCCESS!")
}
예제 #4
0
func deleteDomains(database *mgo.Database) {
	mux := handy.NewHandy()

	h := new(handler.DomainHandler)
	mux.Handle("/domain/{fqdn}", func() handy.Handler {
		return h
	})

	for i := 0; i < 100; i++ {
		r, err := http.NewRequest("DELETE", fmt.Sprintf("/domain/example%d.com.br.", i), nil)
		if err != nil {
			utils.Fatalln("Error creating the HTTP request", err)
		}
		utils.BuildHTTPHeader(r, nil)

		w := httptest.NewRecorder()
		mux.ServeHTTP(w, r)

		responseContent, err := ioutil.ReadAll(w.Body)
		if err != nil {
			utils.Fatalln("Error reading response body", err)
		}

		if w.Code != http.StatusNoContent {
			utils.Fatalln("Error removing domain", errors.New(string(responseContent)))
		}
	}
}
예제 #5
0
func retrieveUnknownDomain(database *mgo.Database) {
	mux := handy.NewHandy()

	h := new(handler.DomainHandler)
	mux.Handle("/domain/{fqdn}", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", "/domain/example.com.br.", nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusNotFound {
		utils.Fatalln("Error retrieving unknown domain", errors.New(string(responseContent)))
	}
}
예제 #6
0
func createDomains() {
	var client http.Client

	url := ""
	if len(config.ShelterConfig.WebClient.Listeners) > 0 {
		url = fmt.Sprintf("http://%s:%d", config.ShelterConfig.WebClient.Listeners[0].IP,
			config.ShelterConfig.WebClient.Listeners[0].Port)
	}

	if len(url) == 0 {
		utils.Fatalln("There's no interface to connect to", nil)
	}

	var r *http.Request
	var err error

	for i := 0; i < 100; i++ {
		uri := fmt.Sprintf("/domain/example%d.com.br.", i)
		content := `{
  "Nameservers": [
    { "host": "a.dns.br." },
    { "host": "b.dns.br." }
  ],
  "Owners": [
    { "email": "*****@*****.**", "language": "pt-br" }
  ]
}`

		r, err = http.NewRequest("PUT", fmt.Sprintf("%s%s", url, uri),
			bytes.NewReader([]byte(content)))

		if err != nil {
			utils.Fatalln("Error creating the HTTP request", err)
		}

		utils.BuildHTTPHeader(r, []byte(content))

		response, err := client.Do(r)
		if err != nil {
			utils.Fatalln("Error sending request", err)
		}

		if response.StatusCode != http.StatusCreated {
			responseContent, err := ioutil.ReadAll(response.Body)

			if err == nil {
				utils.Fatalln(fmt.Sprintf("Expected HTTP status %d and got %d for method PUT and URI %s",
					http.StatusCreated, response.StatusCode, uri),
					errors.New(string(responseContent)),
				)
			} else {
				utils.Fatalln(fmt.Sprintf("Expected HTTP status %d and got %d for method PUT and URI %s",
					http.StatusCreated, response.StatusCode, uri), nil)
			}
		}
	}
}
예제 #7
0
func retrieveScan(database *mgo.Database, startedAt time.Time) {
	mux := handy.NewHandy()

	h := new(handler.ScanHandler)
	mux.Handle("/scan/{started-at}", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", fmt.Sprintf("/scan/%s", startedAt.Format(time.RFC3339Nano)), nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusOK {
		utils.Fatalln("Error retrieving scan", errors.New(string(responseContent)))
	}

	if w.Header().Get("ETag") != "1" {
		utils.Fatalln("Not setting ETag in scan retrieve response", nil)
	}

	if len(w.Header().Get("Last-Modified")) == 0 {
		utils.Fatalln("Not setting Last-Modified in scan retrieve response", nil)
	}

	if h.Response.Status != "EXECUTED" {
		utils.Fatalln("Scan's status was not persisted correctly", nil)
	}

	if h.Response.DomainsScanned != 5000000 {
		utils.Fatalln("Scan's domains scanned information is wrong", nil)
	}

	if h.Response.DomainsWithDNSSECScanned != 250000 {
		utils.Fatalln("Scan's domains with DNSSEC scanned information is wrong", nil)
	}

	if h.Response.NameserverStatistics["OK"] != 4800000 ||
		h.Response.NameserverStatistics["TIMEOUT"] != 200000 {
		utils.Fatalln("Scan's nameserver statistics are wrong", nil)
	}

	if h.Response.DSStatistics["OK"] != 220000 ||
		h.Response.DSStatistics["EXPSIG"] != 30000 {
		utils.Fatalln("Scan's nameserver statistics are wrong", nil)
	}
}
예제 #8
0
func main() {
	flag.Parse()

	err := utils.ReadConfigFile(configFilePath, &config.ShelterConfig)

	if err == utils.ErrConfigFileUndefined {
		fmt.Println(err.Error())
		fmt.Println("Usage:")
		flag.PrintDefaults()
		return

	} else if err != nil {
		utils.Fatalln("Error reading configuration file", err)
	}

	database, databaseSession, err := mongodb.Open(
		config.ShelterConfig.Database.URIs,
		config.ShelterConfig.Database.Name,
		false, "", "",
	)

	if err != nil {
		utils.Fatalln("Error connecting the database", err)
	}
	defer databaseSession.Close()

	// If there was some problem in the last test, there could be some data in the
	// database, so let's clear it to don't affect this test. We avoid checking the error,
	// because if the collection does not exist yet, it will be created in the first
	// insert
	utils.ClearDatabase(database)

	invalidFQDN(database)
	createDomain(database)
	updateDomain(database)
	retrieveDomain(database)
	retrieveDomainMetadata(database)
	retrieveDomainIfModifiedSince(database)
	retrieveDomainIfUnmodifiedSince(database)
	retrieveDomainIfMatch(database)
	retrieveDomainIfNoneMatch(database)
	updateDomainIfModifiedSince(database)
	updateDomainIfUnmodifiedSince(database)
	updateDomainIfMatch(database)
	updateDomainIfNoneMatch(database)
	deleteDomainIfModifiedSince(database)
	deleteDomainIfUnmodifiedSince(database)
	deleteDomainIfMatch(database)
	deleteDomainIfNoneMatch(database)
	deleteDomain(database)
	retrieveUnknownDomain(database)

	utils.Println("SUCCESS!")
}
예제 #9
0
func main() {
	flag.Parse()

	var config DomainDAOTestConfigFile
	err := utils.ReadConfigFile(configFilePath, &config)

	if err == utils.ErrConfigFileUndefined {
		fmt.Println(err.Error())
		fmt.Println("Usage:")
		flag.PrintDefaults()
		return

	} else if err != nil {
		utils.Fatalln("Error reading configuration file", err)
	}

	database, databaseSession, err := mongodb.Open(
		[]string{config.Database.URI},
		config.Database.Name,
		false, "", "",
	)

	if err != nil {
		utils.Fatalln("Error connecting the database", err)
	}
	defer databaseSession.Close()

	domainDAO := dao.DomainDAO{
		Database: database,
	}

	// If there was some problem in the last test, there could be some data in the
	// database, so let's clear it to don't affect this test. We avoid checking the error,
	// because if the collection does not exist yet, it will be created in the first
	// insert
	domainDAO.RemoveAll()

	domainLifeCycle(domainDAO)
	domainsLifeCycle(domainDAO)
	domainUniqueFQDN(domainDAO)
	domainConcurrency(domainDAO)
	domainsPagination(domainDAO)
	domainsNotification(domainDAO)
	domainsExpand(domainDAO)
	domainFilter(domainDAO)

	// Domain DAO performance report is optional and only generated when the report file
	// path parameter is given
	if report {
		domainDAOPerformanceReport(config.Report.ReportFile, domainDAO)
	}

	utils.Println("SUCCESS!")
}
예제 #10
0
func main() {
	flag.Parse()

	err := utils.ReadConfigFile(configFilePath, &config.ShelterConfig)

	if err == utils.ErrConfigFileUndefined {
		fmt.Println(err.Error())
		fmt.Println("Usage:")
		flag.PrintDefaults()
		return

	} else if err != nil {
		utils.Fatalln("Error reading configuration file", err)
	}

	database, databaseSession, err := mongodb.Open(
		config.ShelterConfig.Database.URIs,
		config.ShelterConfig.Database.Name,
		config.ShelterConfig.Database.Auth.Enabled,
		config.ShelterConfig.Database.Auth.Username,
		config.ShelterConfig.Database.Auth.Password,
	)

	if err != nil {
		utils.Fatalln("Error connecting the database", err)
	}
	defer databaseSession.Close()

	// If there was some problem in the last test, there could be some data in the database,
	// so let's clear it to don't affect this test. We avoid checking the error, because if
	// the collection does not exist yet, it will be created in the first insert
	utils.ClearDatabase(database)

	messageChannel, errorChannel, err := utils.StartMailServer(config.ShelterConfig.Notification.SMTPServer.Port)
	if err != nil {
		utils.Fatalln("Error starting the mail server", err)
	}

	domainDAO := dao.DomainDAO{
		Database: database,
	}

	templateName := createTemplateFile()
	defer removeTemplateFile(templateName)

	simpleNotification(domainDAO, templateName, messageChannel, errorChannel)

	utils.Println("SUCCESS!")
}
예제 #11
0
파일: rest.go 프로젝트: rafaeljusto/shelter
func restActionReport(numberOfItems int,
	content []byte,
	requestBuilder func(int) (*http.Request, error),
	expectedStatus int,
	action string) string {

	var client http.Client

	totalDuration, domainsPerSecond := calculateRESTDurations(func() {
		for i := 0; i < numberOfItems; i++ {
			r, err := requestBuilder(i)
			if err != nil {
				utils.Fatalln("Error creating the HTTP request", err)
			}

			utils.BuildHTTPHeader(r, content)

			response, err := client.Do(r)
			if err != nil {
				utils.Fatalln(fmt.Sprintf("Error detected when sending request for action %s"+
					" and URI \"%s\"", action, r.URL.RequestURI()), err)
			}

			_, err = ioutil.ReadAll(response.Body)
			if err != nil {
				utils.Fatalln(fmt.Sprintf("Error detected when reading the response body for action %s"+
					" and URI \"%s\"", action, r.URL.RequestURI()), err)
			}
			response.Body.Close()

			if response.StatusCode != expectedStatus {
				utils.Fatalln(fmt.Sprintf("Error with the domain object in the action %s. "+
					"Expected HTTP status %d and got %d",
					action, expectedStatus, response.StatusCode), nil)
			}
		}
	}, numberOfItems)

	var memStats runtime.MemStats
	runtime.ReadMemStats(&memStats)

	return fmt.Sprintf("% -8d | %-9s | %16s | %4d | %14.2f\n",
		numberOfItems,
		action,
		time.Duration(int64(totalDuration)).String(),
		domainsPerSecond,
		float64(memStats.Alloc)/float64(MB),
	)
}
예제 #12
0
func updateDomainIfNoneMatch(database *mgo.Database) {
	requestContent := `{
      "Nameservers": [
        { "host": "ns1.example.com.br.", "ipv4": "127.0.0.1" },
        { "host": "ns3.example.com.br.", "ipv6": "::1" }
      ],
      "Owners": [
        { "email": "*****@*****.**", "language": "en-us" }
      ]
    }`

	r, err := http.NewRequest("PUT", "/domain/example.com.br.", strings.NewReader(requestContent))
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}

	domainCacheTest(database, r, requestContent, "If-None-Match", []DomainCacheTestData{
		{
			HeaderValue:        "3",
			ExpectedHTTPStatus: http.StatusPreconditionFailed,
		},
		{
			HeaderValue:        "abcdef",
			ExpectedHTTPStatus: http.StatusNoContent,
		},
	})
}
func main() {
	flag.Parse()

	var clientConfig ClientDomainVerificationTestConfigFile
	err := utils.ReadConfigFile(configFilePath, &clientConfig)
	config.ShelterConfig = clientConfig.Config

	if err == utils.ErrConfigFileUndefined {
		fmt.Println(err.Error())
		fmt.Println("Usage:")
		flag.PrintDefaults()
		return

	} else if err != nil {
		utils.Fatalln("Error reading configuration file", err)
	}

	utils.StartDNSServer(clientConfig.DNSServerPort, clientConfig.Scan.UDPMaxSize)
	finishRESTServer := utils.StartRESTServer()
	defer finishRESTServer()
	utils.StartWebClient()

	scanDomain()
	queryDomain()

	utils.Println("SUCCESS!")
}
예제 #14
0
// Function to mock a domain
func generateAndSaveDomain(fqdn string, domainDAO dao.DomainDAO, language string) {
	lastOKAt := time.Now().Add(time.Duration(-config.ShelterConfig.Notification.NameserverErrorAlertDays*24) * time.Hour)
	owner, _ := mail.ParseAddress("*****@*****.**")

	domain := model.Domain{
		FQDN: fqdn,
		Nameservers: []model.Nameserver{
			{
				Host:       fmt.Sprintf("ns1.%s", fqdn),
				IPv4:       net.ParseIP("127.0.0.1"),
				LastStatus: model.NameserverStatusServerFailure,
				LastOKAt:   lastOKAt,
			},
		},
		Owners: []model.Owner{
			{
				Email:    owner,
				Language: language,
			},
		},
	}

	if err := domainDAO.Save(&domain); err != nil {
		utils.Fatalln(fmt.Sprintf("Fail to save domain %s", domain.FQDN), err)
	}
}
예제 #15
0
func main() {
	flag.Parse()

	var config ScanQuerierTestConfigFile
	err := utils.ReadConfigFile(configFilePath, &config)

	if err == utils.ErrConfigFileUndefined {
		fmt.Println(err.Error())
		fmt.Println("Usage:")
		flag.PrintDefaults()
		return

	} else if err != nil {
		utils.Fatalln("Error reading configuration file", err)
	}

	utils.StartDNSServer(config.Server.Port, config.Scan.UDPMaxSize)

	domainWithNoDNSErrors(config)
	domainWithNoDNSSECErrors(config)
	domainDNSTimeout(config)
	domainDNSUnknownHost(config)

	// Scan querier performance report is optional and only generated when the report file
	// path parameter is given
	if report {
		scanQuerierReport(config)
	}

	if inputReport {
		inputScanReport(config)
	}

	utils.Println("SUCCESS!")
}
예제 #16
0
func createScan(database *mgo.Database) time.Time {
	scan := model.Scan{
		Status:                   model.ScanStatusExecuted,
		StartedAt:                time.Now().Add(-60 * time.Minute),
		FinishedAt:               time.Now().Add(-10 * time.Minute),
		DomainsScanned:           5000000,
		DomainsWithDNSSECScanned: 250000,
		NameserverStatistics: map[string]uint64{
			"OK":      4800000,
			"TIMEOUT": 200000,
		},
		DSStatistics: map[string]uint64{
			"OK":     220000,
			"EXPSIG": 30000,
		},
	}

	scanDAO := dao.ScanDAO{
		Database: database,
	}

	if err := scanDAO.Save(&scan); err != nil {
		utils.Fatalln("Error creating scan", err)
	}

	return scan.StartedAt
}
예제 #17
0
func retrieveScansIfNoneMatch(database *mgo.Database) {
	mux := handy.NewHandy()

	h := new(handler.ScansHandler)
	mux.Handle("/scans", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", "/scans", nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	scansCacheTest(database, r, "If-None-Match", []ScansCacheTestData{
		{
			HeaderValue:        w.Header().Get("ETag"),
			ExpectedHTTPStatus: http.StatusNotModified,
		},
		{
			HeaderValue:        w.Header().Get("ETag") + "x",
			ExpectedHTTPStatus: http.StatusOK,
		},
	})
}
예제 #18
0
func createScans(database *mgo.Database) {
	scanDAO := dao.ScanDAO{
		Database: database,
	}

	for i := 0; i < 100; i++ {
		scan := model.Scan{
			Status:                   model.ScanStatusExecuted,
			StartedAt:                time.Now().Add(time.Duration(-i*2) * time.Minute),
			FinishedAt:               time.Now().Add(time.Duration(-i) * time.Minute),
			DomainsScanned:           5000000,
			DomainsWithDNSSECScanned: 250000,
			NameserverStatistics: map[string]uint64{
				"OK":      4800000,
				"TIMEOUT": 200000,
			},
			DSStatistics: map[string]uint64{
				"OK":     220000,
				"EXPSIG": 30000,
			},
		}

		if err := scanDAO.Save(&scan); err != nil {
			utils.Fatalln("Error creating scan", err)
		}
	}
}
예제 #19
0
func updateDomainIfUnmodifiedSince(database *mgo.Database) {
	requestContent := `{
      "Nameservers": [
        { "host": "ns1.example.com.br.", "ipv4": "127.0.0.1" },
        { "host": "ns3.example.com.br.", "ipv6": "::1" }
      ],
      "Owners": [
        { "email": "*****@*****.**", "language": "pt-br" }
      ]
    }`

	r, err := http.NewRequest("PUT", "/domain/example.com.br.", strings.NewReader(requestContent))
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}

	domainCacheTest(database, r, requestContent, "If-Unmodified-Since", []DomainCacheTestData{
		{
			HeaderValue:        "abcdef",
			ExpectedHTTPStatus: http.StatusBadRequest,
		},
		{
			HeaderValue:        time.Now().Add(-10 * time.Second).UTC().Format(time.RFC1123),
			ExpectedHTTPStatus: http.StatusPreconditionFailed,
		},
	})
}
예제 #20
0
func removeDomains() {
	var client http.Client

	url := ""
	if len(config.ShelterConfig.WebClient.Listeners) > 0 {
		url = fmt.Sprintf("http://%s:%d", config.ShelterConfig.WebClient.Listeners[0].IP,
			config.ShelterConfig.WebClient.Listeners[0].Port)
	}

	if len(url) == 0 {
		utils.Fatalln("There's no interface to connect to", nil)
	}

	var r *http.Request
	var err error

	for i := 0; i < 100; i++ {
		uri := fmt.Sprintf("/domain/example%d.com.br.", i)

		r, err = http.NewRequest("DELETE", fmt.Sprintf("%s%s", url, uri), nil)

		if err != nil {
			utils.Fatalln("Error creating the HTTP request", err)
		}

		utils.BuildHTTPHeader(r, nil)

		response, err := client.Do(r)
		if err != nil {
			utils.Fatalln("Error sending request", err)
		}

		if response.StatusCode != http.StatusNoContent {
			responseContent, err := ioutil.ReadAll(response.Body)

			if err == nil {
				utils.Fatalln(fmt.Sprintf("Expected HTTP status %d and got %d for method DELETE and URI %s",
					http.StatusCreated, response.StatusCode, uri),
					errors.New(string(responseContent)),
				)
			} else {
				utils.Fatalln(fmt.Sprintf("Expected HTTP status %d and got %d for method DELETE and URI %s",
					http.StatusCreated, response.StatusCode, uri), nil)
			}
		}
	}
}
func main() {
	flag.Parse()

	var restConfig RESTHandlerDomainVerificationTestConfigFile
	err := utils.ReadConfigFile(configFilePath, &restConfig)
	config.ShelterConfig = restConfig.Config

	if err == utils.ErrConfigFileUndefined {
		fmt.Println(err.Error())
		fmt.Println("Usage:")
		flag.PrintDefaults()
		return

	} else if err != nil {
		utils.Fatalln("Error reading configuration file", err)
	}

	database, databaseSession, err := mongodb.Open(
		restConfig.Database.URIs,
		restConfig.Database.Name,
		false, "", "",
	)

	if err != nil {
		utils.Fatalln("Error connecting the database", err)
	}
	defer databaseSession.Close()

	domainDAO := dao.DomainDAO{
		Database: database,
	}

	// If there was some problem in the last test, there could be some data in the
	// database, so let's clear it to don't affect this test. We avoid checking the error,
	// because if the collection does not exist yet, it will be created in the first
	// insert
	domainDAO.RemoveAll()

	utils.StartDNSServer(restConfig.DNSServerPort, restConfig.Scan.UDPMaxSize)

	scanDomain()
	scanPersistedDomain(domainDAO)
	queryDomain()

	utils.Println("SUCCESS!")
}
예제 #22
0
func main() {
	flag.Parse()

	err := utils.ReadConfigFile(configFilePath, &config.ShelterConfig)

	if err == utils.ErrConfigFileUndefined {
		fmt.Println(err.Error())
		fmt.Println("Usage:")
		flag.PrintDefaults()
		return

	} else if err != nil {
		utils.Fatalln("Error reading configuration file", err)
	}

	finishRESTServer := utils.StartRESTServer()
	defer finishRESTServer()
	utils.StartWebClient()

	database, databaseSession, err := mongodb.Open(
		config.ShelterConfig.Database.URIs,
		config.ShelterConfig.Database.Name,
		config.ShelterConfig.Database.Auth.Enabled,
		config.ShelterConfig.Database.Auth.Username,
		config.ShelterConfig.Database.Auth.Password,
	)

	if err != nil {
		utils.Fatalln("Error connecting the database", err)
	}
	defer databaseSession.Close()

	// If there was some problem in the last test, there could be some data in the
	// database, so let's clear it to don't affect this test. We avoid checking the error,
	// because if the collection does not exist yet, it will be created in the first
	// insert
	utils.ClearDatabase(database)

	createScans(database)
	retrieveScans()
	retrieveScansWithPagination()
	retrieveScansWithCache()
	retrieveCurrentScan()

	utils.Println("SUCCESS!")
}
예제 #23
0
func main() {
	flag.Parse()

	var config ScanDAOTestConfigFile
	err := utils.ReadConfigFile(configFilePath, &config)

	if err == utils.ErrConfigFileUndefined {
		fmt.Println(err.Error())
		fmt.Println("Usage:")
		flag.PrintDefaults()
		return

	} else if err != nil {
		utils.Fatalln("Error reading configuration file", err)
	}

	database, databaseSession, err := mongodb.Open(
		[]string{config.Database.URI},
		config.Database.Name,
		false, "", "",
	)

	if err != nil {
		utils.Fatalln("Error connecting the database", err)
	}
	defer databaseSession.Close()

	scanDAO := dao.ScanDAO{
		Database: database,
	}

	// If there was some problem in the last test, there could be some data in the
	// database, so let's clear it to don't affect this test. We avoid checking the error,
	// because if the collection does not exist yet, it will be created in the first
	// insert
	scanDAO.RemoveAll()

	scanLifeCycle(scanDAO)
	scanConcurrency(scanDAO)
	scanStatistics(scanDAO)
	scansPagination(scanDAO)
	scansExpand(scanDAO)

	utils.Println("SUCCESS!")
}
예제 #24
0
func domainFilter(domainDAO dao.DomainDAO) {
	numberOfItems := 20

	for i := 0; i < numberOfItems; i++ {
		domain := model.Domain{
			FQDN: fmt.Sprintf("example%d.com.br", i),
		}

		if err := domainDAO.Save(&domain); err != nil {
			utils.Fatalln("Error saving domain in database", err)
		}
	}

	pagination := dao.DomainDAOPagination{
		PageSize: 10,
		Page:     5,
		OrderBy: []dao.DomainDAOSort{
			{
				Field:     dao.DomainDAOOrderByFieldFQDN,
				Direction: dao.DAOOrderByDirectionAscending,
			},
		},
	}

	domains, err := domainDAO.FindAll(&pagination, true, "example1\\.com.*")
	if err != nil {
		utils.Fatalln("Error retrieving domains", err)
	}

	if len(domains) != 1 {
		utils.Fatalln(fmt.Sprintf("Wrong number of domains when there's filter. "+
			"Expected '1' and got '%d'", len(domains)), nil)
	}

	if domains[0].FQDN != "example1.com.br" {
		utils.Fatalln("Wrong domain returned", nil)
	}

	for i := 0; i < numberOfItems; i++ {
		fqdn := fmt.Sprintf("example%d.com.br", i)
		if err := domainDAO.RemoveByFQDN(fqdn); err != nil {
			utils.Fatalln("Error removing domain from database", err)
		}
	}
}
예제 #25
0
func deleteScan(database *mgo.Database, startedAt time.Time) {
	scanDAO := dao.ScanDAO{
		Database: database,
	}

	if err := scanDAO.RemoveByStartedAt(startedAt); err != nil {
		utils.Fatalln("Error removing scan", err)
	}
}
예제 #26
0
func deleteScans(database *mgo.Database) {
	scanDAO := dao.ScanDAO{
		Database: database,
	}

	if err := scanDAO.RemoveAll(); err != nil {
		utils.Fatalln("Error removing scans", err)
	}
}
예제 #27
0
파일: scan.go 프로젝트: rafaeljusto/shelter
func generateAndSignDomain(fqdn string) (
	model.Domain, *dns.DNSKEY, *dns.RRSIG, time.Time, time.Time,
) {
	dnskey, rrsig, err := utils.GenerateKSKAndSignZone(fqdn)
	if err != nil {
		utils.Fatalln("Error creating KSK DNSSEC keys and signatures", err)
	}
	ds := dnskey.ToDS(uint8(model.DSDigestTypeSHA1))

	domain := model.Domain{
		FQDN: fqdn,
		Nameservers: []model.Nameserver{
			{
				Host: fmt.Sprintf("ns1.%s", fqdn),
				IPv4: net.ParseIP("127.0.0.1"),
			},
		},
		DSSet: []model.DS{
			{
				Keytag:     dnskey.KeyTag(),
				Algorithm:  utils.ConvertKeyAlgorithm(dnskey.Algorithm),
				DigestType: model.DSDigestTypeSHA1,
				Digest:     ds.Digest,
			},
		},
	}

	owner, _ := mail.ParseAddress("*****@*****.**")
	domain.Owners = []model.Owner{
		{
			Email:    owner,
			Language: "pt-BR",
		},
	}

	lastCheckAt := time.Now().Add(-72 * time.Hour)
	lastOKAt := lastCheckAt.Add(-24 * time.Hour)

	// Set all nameservers with error and the last check equal of the error check interval,
	// this will force the domain to be checked
	for index, _ := range domain.Nameservers {
		domain.Nameservers[index].LastCheckAt = lastCheckAt
		domain.Nameservers[index].LastOKAt = lastOKAt
		domain.Nameservers[index].LastStatus = model.NameserverStatusServerFailure
	}

	// Set all DS records with error and the last check equal of the error check interval,
	// this will force the domain to be checked
	for index, _ := range domain.DSSet {
		domain.DSSet[index].LastCheckAt = lastCheckAt
		domain.DSSet[index].LastOKAt = lastOKAt
		domain.DSSet[index].LastStatus = model.DSStatusTimeout
	}

	return domain, dnskey, rrsig, lastCheckAt, lastOKAt
}
예제 #28
0
func retrieveCurrentScan() {
	var client http.Client

	url := ""
	if len(config.ShelterConfig.WebClient.Listeners) > 0 {
		url = fmt.Sprintf("http://%s:%d", config.ShelterConfig.WebClient.Listeners[0].IP,
			config.ShelterConfig.WebClient.Listeners[0].Port)
	}

	if len(url) == 0 {
		utils.Fatalln("There's no interface to connect to", nil)
	}

	r, err := http.NewRequest("GET", fmt.Sprintf("%s%s", url, "/scans/?current"), nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}

	utils.BuildHTTPHeader(r, nil)

	response, err := client.Do(r)
	if err != nil {
		utils.Fatalln("Error sending request", err)
	}

	responseContent, err := ioutil.ReadAll(response.Body)
	if err != nil {
		utils.Fatalln("Error reading response content", err)
	}

	if response.StatusCode != http.StatusOK {
		utils.Fatalln(fmt.Sprintf("Expected HTTP status %d and got %d for method GET "+
			"and URI /scan/currentscan",
			http.StatusOK, response.StatusCode),
			errors.New(string(responseContent)),
		)
	}

	var scansResponse protocol.ScansResponse
	if err := json.Unmarshal(responseContent, &scansResponse); err != nil {
		utils.Fatalln("Error decoding scan response", err)
	}

	if len(scansResponse.Scans) != 1 {
		utils.Fatalln("Not returning the current scan in the result set or "+
			"more items than expected were found", nil)
	}

	if scansResponse.Scans[0].Status != model.ScanStatusToString(model.ScanStatusWaitingExecution) {
		utils.Fatalln(fmt.Sprintf("Invalid status returned by current scan. Expected %s and got %s",
			model.ScanStatusToString(model.ScanStatusWaitingExecution), scansResponse.Scans[0].Status), nil)
	}
}
예제 #29
0
파일: scan.go 프로젝트: rafaeljusto/shelter
// Generates a report with the amount of time of a scan
func scanReport(domainDAO dao.DomainDAO, scanDAO dao.ScanDAO, scanConfig ScanTestConfigFile) {
	report := " #       | Total            | DPS  | Memory (MB)\n" +
		"-----------------------------------------------------\n"

	// Report variables
	scale := []int{10, 50, 100, 500, 1000, 5000,
		10000, 50000, 100000, 500000, 1000000, 5000000}

	dnskey, privateKey, err := utils.GenerateKey()
	if err != nil {
		utils.Fatalln("Error generating DNSKEY", err)
	}

	reportHandler := ReportHandler{
		DNSKEY:     dnskey,
		PrivateKey: privateKey,
	}

	server.Handler = reportHandler
	dns.DefaultServeMux = nil

	for _, numberOfItems := range scale {
		utils.Println(fmt.Sprintf("Generating report - scale %d", numberOfItems))

		for i := 0; i < numberOfItems; i++ {
			if i%1000 == 0 {
				utils.PrintProgress("building scenario", (i*100)/numberOfItems)
			}

			fqdn := fmt.Sprintf("domain%d.br.", i)
			generateAndSaveDomain(fqdn, domainDAO, dnskey)
		}

		utils.PrintProgress("building scenario", 100)
		totalDuration, domainsPerSecond := calculateScanDurations(numberOfItems, scanDAO)

		var memStats runtime.MemStats
		runtime.ReadMemStats(&memStats)

		report += fmt.Sprintf("% -8d | %16s | %4d | %14.2f\n",
			numberOfItems,
			time.Duration(int64(totalDuration)).String(),
			domainsPerSecond,
			float64(memStats.Alloc)/float64(MB),
		)

		if err := domainDAO.RemoveAll(); err != nil {
			// When the result set is too big to remove, we got a timeout error from the
			// connection, but it's ok
			//utils.Fatalln("Error removing domains generated for report", err)
		}
	}

	utils.WriteReport(scanConfig.Report.File, report)
}
예제 #30
0
func retrieveDomain(database *mgo.Database) {
	mux := handy.NewHandy()

	h := new(handler.DomainHandler)
	mux.Handle("/domain/{fqdn}", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", "/domain/example.com.br.", nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusOK {
		utils.Fatalln("Error retrieving domain", errors.New(string(responseContent)))
	}

	if w.Header().Get("ETag") != "2" {
		utils.Fatalln("Not setting ETag in domain retrieve response", nil)
	}

	if len(w.Header().Get("Last-Modified")) == 0 {
		utils.Fatalln("Not setting Last-Modified in domain retrieve response", nil)
	}

	if h.Response.FQDN != "example.com.br." {
		utils.Fatalln("Domain's FQDN was not persisted correctly", nil)
	}

	if len(h.Response.Nameservers) != 2 ||
		h.Response.Nameservers[0].Host != "ns1.example.com.br." ||
		h.Response.Nameservers[0].IPv4 != "127.0.0.1" ||
		h.Response.Nameservers[1].Host != "ns3.example.com.br." ||
		h.Response.Nameservers[1].IPv6 != "::1" {
		utils.Fatalln("Domain's nameservers were not persisted correctly", nil)
	}

	if len(h.Response.Owners) != 1 ||
		h.Response.Owners[0].Email != "*****@*****.**" {

		utils.Fatalln("Domain's owners were not persisted correctly", nil)
	}
}