-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Enable download of GeoLite2 databases #4896
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package nginx | ||
|
||
import ( | ||
"archive/tar" | ||
"compress/gzip" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"path" | ||
"strings" | ||
) | ||
|
||
// MaxmindLicenseKey maxmind license key to download databases | ||
var MaxmindLicenseKey = "" | ||
|
||
const ( | ||
geoIPPath = "/etc/nginx/geoip" | ||
|
||
geoLiteCityDB = "GeoLite2-City" | ||
geoLiteASNDB = "GeoLite2-ASN" | ||
|
||
dbExtension = ".mmdb" | ||
|
||
maxmindURL = "https://download.maxmind.com/app/geoip_download?license_key=%v&edition_id=%v&suffix=tar.gz" | ||
) | ||
|
||
// GeoLite2DBExists checks if the required databases for | ||
// the GeoIP2 NGINX module are present in the filesystem | ||
func GeoLite2DBExists() bool { | ||
if !fileExists(path.Join(geoIPPath, geoLiteASNDB+dbExtension)) { | ||
return false | ||
} | ||
|
||
if !fileExists(path.Join(geoIPPath, geoLiteCityDB+dbExtension)) { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
// DownloadGeoLite2DB downloads the required databases by the | ||
// GeoIP2 NGINX module using a license key from MaxMind. | ||
func DownloadGeoLite2DB() error { | ||
err := downloadDatabase(geoLiteCityDB) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = downloadDatabase(geoLiteASNDB) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func downloadDatabase(dbName string) error { | ||
url := fmt.Sprintf(maxmindURL, MaxmindLicenseKey, dbName) | ||
req, err := http.NewRequest(http.MethodGet, url, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("HTTP status %v", resp.Status) | ||
} | ||
|
||
archive, err := gzip.NewReader(resp.Body) | ||
if err != nil { | ||
return err | ||
} | ||
defer archive.Close() | ||
|
||
mmdbFile := dbName + dbExtension | ||
|
||
tarReader := tar.NewReader(archive) | ||
for true { | ||
header, err := tarReader.Next() | ||
if err == io.EOF { | ||
break | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
switch header.Typeflag { | ||
case tar.TypeReg: | ||
if !strings.HasSuffix(header.Name, mmdbFile) { | ||
continue | ||
} | ||
|
||
outFile, err := os.Create(path.Join(geoIPPath, mmdbFile)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer outFile.Close() | ||
|
||
if _, err := io.Copy(outFile, tarReader); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
return fmt.Errorf("the URL %v does not contains the database %v", | ||
fmt.Sprintf(maxmindURL, "XXXXXXX", dbName), mmdbFile) | ||
} | ||
|
||
func fileExists(filePath string) bool { | ||
info, err := os.Stat(filePath) | ||
if os.IsNotExist(err) { | ||
return false | ||
} | ||
|
||
return !info.IsDir() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commercial GeoIP2 databases are more accurate than GeoLite2 ones. We use
GeoIP2-ISP
instead ofGeoLite2-ASN
. So with this design I had to create a placeholderGeoLite2-ASN.mmdb
file to trick the controller into believing this file exist so that it does not force disableuse-geoip2
setting.I think instead of this the controller should get the comma separated list of DB file names as a command line arrgument and then check for existence of them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should be more dramatic than that. Right now those files are present in the docker image and are thirteen months old. The old geoip module databases are not maintained anymore.
Before any change, I think we should consider drop support for the old geoip module.