public string GetSignature(Lease lease) { Lease lastLease = (from l in this.Leases orderby l.LeaseId descending select l).FirstOrDefault(); StringWriter stringWriter = new StringWriter(); stringWriter.Write(lastLease != null ? lastLease.HMAC : ""); stringWriter.Write(';'); lease.Write(stringWriter, false); using (HMAC hmac = HMAC.Create()) { return Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringWriter.ToString()))); } }
private bool FixLease(Lease lease, DateTime time, bool fixEndTime = true) { PostSharp.Sdk.Extensibility.Licensing.License parsedLicense = ParsedLicenseManager.GetParsedLicense(lease.License.LicenseKey); if (lease.EndTime <= lease.StartTime) { throw new Exception("Assertion failed."); } if (fixEndTime) { if (parsedLicense.ValidTo.HasValue && parsedLicense.ValidTo < lease.EndTime) { lease.EndTime = parsedLicense.ValidTo.Value; } if (lease.Grace) { DateTime graceEnd = lease.License.GraceStartTime.Value.AddDays(parsedLicense.GetGraceDaysOrDefault()); if (lease.EndTime > graceEnd) { lease.EndTime = graceEnd; } } if (lease.EndTime <= time) { return(false); } if (lease.EndTime <= lease.StartTime) { throw new Exception("Assertion failed."); } } lease.HMAC = this.GetSignature(lease); return(true); }
public LicenseLease GetLicenseLease(Database db, string productCode, Version version, DateTime?buildDate, string machine, string userName, string authenticatedUserName, DateTime now, Dictionary <int, string> errors) { Settings settings = Settings.Default; // Get suitable active licenses. License[] licenses = (from license in db.Licenses where license.ProductCode == productCode && license.Priority >= 0 orderby license.Priority select license).ToArray(); // Check if the machine belongs to build server user. if (IsBuildServer(machine)) { License buildServerLicense = GetBuildServerLicense(licenses); if (buildServerLicense != null) { DateTime endTime = now.AddDays(Settings.Default.NewLeaseDays); LicenseLease buildServerLicenseLease = new LicenseLease(buildServerLicense.LicenseKey, now, endTime, endTime.AddDays(-settings.MinLeaseDays)); // Lease for build server should not be stored - build server users shouldn't steal developer licenses. return(buildServerLicenseLease); } // try to acquire lease in normal way } Lease lease = GetLease(db, productCode, version, buildDate, machine, userName, authenticatedUserName, now, errors, licenses); if (lease == null) { return(null); } LicenseLease licenseLease = new LicenseLease(lease.License.LicenseKey, lease.StartTime, lease.EndTime, lease.EndTime.AddDays(-settings.MinLeaseDays)); return(licenseLease); }
public Lease ProlongLease(Lease oldLease, string authenticatedUserName, DateTime time) { Lease newLease = new Lease { License = oldLease.License, AuthenticatedUser = authenticatedUserName, OverwritesLease = oldLease, StartTime = oldLease.StartTime, EndTime = time.AddDays(Settings.Default.NewLeaseDays), Machine = oldLease.Machine, UserName = oldLease.UserName, Grace = oldLease.Grace }; if (!this.FixLease(newLease, time)) return null; this.Leases.InsertOnSubmit(newLease); LeaseService.CheckAssertions( this, newLease.License, time ); return newLease; }
public Lease CreateLease(License license, string user, string machine, string authenticatedUserName, DateTime time, bool grace) { Lease lease = new Lease { License = license, AuthenticatedUser = authenticatedUserName, EndTime = time.AddDays(Settings.Default.NewLeaseDays), Machine = machine, StartTime = time, UserName = user, Grace = grace }; if (!this.FixLease(lease, time)) return null; this.Leases.InsertOnSubmit((lease)); LeaseService.CheckAssertions(this, license, time); return lease; }
partial void InsertLease(Lease instance);
partial void UpdateLease(Lease instance);
private void attach_Leases(Lease entity) { this.SendPropertyChanging(); entity.License = this; }
private void detach_Leases(Lease entity) { this.SendPropertyChanging(); entity.License = null; }
public void CancelLease(Lease lease, string authenticatedUserName, DateTime time) { Lease overwrite = new Lease { AuthenticatedUser = authenticatedUserName, License = lease.License, UserName = lease.UserName, Machine = lease.Machine, StartTime = lease.StartTime, OverwritesLease = lease, Grace = lease.Grace, EndTime = time, }; this.FixLease( overwrite, time, false ); this.Leases.InsertOnSubmit( overwrite ); }
partial void DeleteLease(Lease instance);
private bool FixLease(Lease lease, DateTime time, bool fixEndTime = true) { PostSharp.Sdk.Extensibility.Licensing.License parsedLicense = ParsedLicenseManager.GetParsedLicense(lease.License.LicenseKey); if (lease.EndTime <= lease.StartTime) throw new Exception( "Assertion failed." ); if (fixEndTime) { if ( parsedLicense.ValidTo.HasValue && parsedLicense.ValidTo < lease.EndTime ) { lease.EndTime = parsedLicense.ValidTo.Value; } if ( lease.Grace ) { DateTime graceEnd = lease.License.GraceStartTime.Value.AddDays( parsedLicense.GetGraceDaysOrDefault() ); if ( lease.EndTime > graceEnd ) { lease.EndTime = graceEnd; } } if ( lease.EndTime <= time ) return false; if (lease.EndTime <= lease.StartTime) throw new Exception("Assertion failed."); } lease.HMAC = this.GetSignature(lease); return true; }
public Lease GetLease(Database db, string productCode, Version version, DateTime?buildDate, string machine, string userName, string authenticatedUserName, DateTime now, Dictionary <int, string> errors, License[] licenses) { Dictionary <int, LicenseState> licenseStates = new Dictionary <int, LicenseState>(); Settings settings = Settings.Default; // Check if we have a lease that we can use. foreach (License license in licenses) { LicenseState licenseState = GetLicenseState(db, license, version, buildDate, now, licenseStates, errors); if (licenseState == null) { continue; } int licenseId = license.LicenseId; Lease[] currentLeases = (from l in db.OpenLeases where l.LicenseId == licenseId && l.StartTime <= now && l.EndTime > now && l.UserName == userName orderby l.StartTime select l).ToArray(); Dictionary <string, string> machines = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); foreach (Lease candidateLease in currentLeases) { machines[candidateLease.Machine] = candidateLease.Machine; if (candidateLease.Machine != machine) { continue; } if (candidateLease.EndTime > now.AddDays(settings.MinLeaseDays)) { // We have already a good lease. return(candidateLease); } else { // Invariant: we can always prolong a lease because licenses are always acquired from // the present moment, therefore taking the current lease into account. // UNLESS it's the end of the license period or the grace period. Lease lease = db.ProlongLease(candidateLease, authenticatedUserName, now); if (lease == null) { continue; } return(lease); } } // We did not find a lease for the requested machines. // See if we can do a new lease that would not increment the number of concurrent users. if (machines.Count % Settings.Default.MachinesPerUser != 0) { CheckAssertions(db, license, now); Lease lease = db.CreateLease(license, userName, machine, authenticatedUserName, now, licenseState.InExcess); if (lease != null) { return(lease); } } } // We don't have a current lease. Create a new one. foreach (License license in licenses) { LicenseState licenseState = GetLicenseState(db, license, version, buildDate, now, licenseStates, errors); if (licenseState == null) { continue; } if (licenseState.Maximum.HasValue && licenseState.Maximum.Value <= licenseState.Usage && (!licenseState.ParsedLicense.ValidTo.HasValue || now < licenseState.ParsedLicense.ValidTo)) { // Not enough users for this license. continue; } Lease lease = db.CreateLease(license, userName, machine, authenticatedUserName, now, false); if (lease != null) { return(lease); } } // We did not find a suitable license. See if we can use the grace period. foreach (License license in licenses) { LicenseState licenseState = GetLicenseState(db, license, version, buildDate, now, licenseStates, errors); if (licenseState == null) { continue; } if (license.GraceStartTime == null) { license.GraceStartTime = now; } int graceLimit = (int)Math.Ceiling(licenseState.Maximum.Value * (100.0 + licenseState.ParsedLicense.GetGracePercentOrDefault()) / 100.0); DateTime graceEnd = license.GraceStartTime.Value.AddDays(licenseState.ParsedLicense.GetGraceDaysOrDefault()); if (license.GraceStartTime <= now && graceEnd > now && licenseState.Usage < graceLimit) { // We will use the grace period. // See if we should send a warning message. if (license.GraceLastWarningTime.GetValueOrDefault(DateTime.MinValue).AddDays(settings.GracePeriodWarningDays) < now) { try { string body = string.Format( "The license #{0} has a capacity of {1} concurrent user(s), but {2} users are currently using the product {3}. " + "The grace period has started on {4} and will end on {5}. After this date, additional leases will be denied." + "Please contact PostSharp Technologies to acquire additional licenses.", license.LicenseId, licenseState.Maximum, licenseState.Usage + 1, licenseState.ParsedLicense.Product, license.GraceStartTime, graceEnd); const string subject = "WARNING: licensing capacity exceeded"; SendEmail(settings.GracePeriodWarningEmailTo, settings.GracePeriodWarningEmailCC, subject, body); license.GraceLastWarningTime = now; } catch (Exception e) { Trace.TraceError(e.ToString()); } } Lease lease = db.CreateLease(license, userName, machine, authenticatedUserName, now, true); if (lease != null) { return(lease); } } } SendEmail(settings.DeniedRequestEmailTo, null, "ERROR: license request denied", string.Format("No license with free capacity was found to satisfy the lease request for the product {0} from " + "the user '{1}' (authentication: '{2}'), machine '{3}'. " + string.Join(". ", errors.ToArray()), productCode, userName, authenticatedUserName, machine)); return(null); }
private void detach_OverwrittenByLease(Lease entity) { this.SendPropertyChanging(); entity.OverwritesLease = null; }